● 1143.最长公共子序列 ● 1035.不相交的线 ● 53. 最大子序和 动态规划

  •  1143.最长公共子序列 
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int res=0;
        vector> dp(text1.size() + 1, vector(text2.size() + 1, 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 - 1][j], dp[i][j - 1]);
                }
                if(dp[i][j]>res)
                res=dp[i][j];
            }
        }
//        return dp[text1.size()][text2.size()];
return res;
    }
};

  •  1035.不相交的线   

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int res=0;
        vector> dp(text1.size() + 1, vector(text2.size() + 1, 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 - 1][j], dp[i][j - 1]);
                }
                if(dp[i][j]>res)
                res=dp[i][j];
            }
        }
//        return dp[text1.size()][text2.size()];
return res;
    }
};

  •  53. 最大子序和  动态规划 
  • class Solution {
    public:
        int maxSubArray(vector& nums) 
        {
            if(nums.size()==0)
            return 0;
            vectordp(nums.size());
            dp[0]=nums[0];
            int res=dp[0];
            for(int i=1;i

你可能感兴趣的:(动态规划,算法)