1143.最长公共子序列
class Solution {
public:
int longestCommonSubsequence(string text1, string text2)
{
vector<vector<int>>dp(text1.size()+1,vector<int>(text2.size()+1,0));
int result=0;
for(int i=1;i<=text1.size();i++)
{
for(int j=1;j<=text2.size();j++)
{
if(text1[i-1]==text2[j-1])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
{
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
result=dp[i][j]>result?dp[i][j]:result;
}
}
return result;
}
};
1035.不相交的线
53. 最大子序和
class Solution {
public:
int maxSubArray(vector<int>& nums)
{
if(nums.size()==1)return nums[0];
int n=nums.size();
vector<int>dp(n,0);
dp[0]=nums[0];
int res=nums[0];
for(int i=1;i<nums.size();i++)
{
dp[i]=max(dp[i-1]+nums[i],nums[i]);
res=max(dp[i],res);
}
return res;
}
};