LeetCode 1143.最长公共子序列
题目链接:
LeetCode 1143.最长公共子序列
代码:
class Solution {
public:int longestCommonSubsequence(string text1, string text2) {int size1=text1.size();int size2=text2.size();int result=0;vector<vector<int>>dp(size1+1,vector<int>(size2+1,0));for(int i=1;i<=size1;i++){for(int j=1;j<=size2;j++){if(text1[i-1]==text2[j-1]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}if(dp[i][j]>result) result=dp[i][j];}}return result;}
};
LeetCode 1035.不相交的线
题目链接:
LeetCode 1035.不相交的线
代码:
class Solution {
public:int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {int size1=nums1.size();int size2=nums2.size();vector<vector<int>>dp(size1+1,vector<int>(size2+1,0));for(int i=1;i<=size1;i++){for(int j=1;j<=size2;j++){if(nums1[i-1]==nums2[j-1]){dp[i][j]=dp[i-1][j-1]+1;}else{dp[i][j] = max(dp[i-1][j],dp[i][j-1]);}}}return dp[size1][size2];}
};
LeetCode 53. 最大子序和
题目链接:
LeetCode 53. 最大子序和
代码:
class Solution {
public:int maxSubArray(vector<int>& nums) {int size = nums.size();if(size==0) return 0;vector<int>dp(nums.size(),0);dp[0]=nums[0];int result = dp[0];for(int i=1;i<size;i++){dp[i] = max(nums[i],dp[i-1]+nums[i]);if(result<dp[i]) result=dp[i];}return result;}
};