- class Solution {
- public:
- int longestCommonSubsequence(string text1, string text2) {
- int n=text1.size(),m=text2.size();
- vector
int>>dp(n+1,vector<int>(m+1,0)); - for(int i=1;i<=n;i++){
- for(int j=1;j<=m;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]);
- //从 0~i-2 和 0~j-1 中,0~i-1 和 0~j-2中,找最长公共子序列
- }
- }
- return dp[n][m];
- }
- };
- class Solution {
- public:
- int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
- int n=nums1.size(),m=nums2.size();
- vector
int>>dp(n+1,vector<int>(m+1,0)); - for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- if(nums1[i-1]==nums2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
- else
- dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
- }
- }
- return dp[n][m];
- }
- };
- class Solution {
- public:
- int maxSubArray(vector<int>& nums) {
- int res=INT_MIN,sum=0;
- for(int i=0;i
size();i++){ - sum+=nums[i];
- if(sum>res) res=sum;
- if(sum<=0) sum=0;
- }
- return res;
- }
- };
- class Solution {
- public:
- int maxSubArray(vector<int>& nums) {
- int n=nums.size();
- vector<int>dp(n,0);
- dp[0]=nums[0];
- int res=dp[0];
- for(int i=1;i
- dp[i]=max(dp[i-1]+nums[i],nums[i]);//最大值在上一位+当前位 和 当前位之间
- if(dp[i]>res) res=dp[i];//更新最大值
- }
- return res;
- }
- };