No.53 - leetCode1143 - 最长公共子序列

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int N = text1.length();
        int M = text2.length();
        int dp[N+1][M+1];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=N;i++){
            for(int j=1;j<=M;j++){
                if(text2[j-1] == text1[i-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[N][M];
    }
};

你可能感兴趣的:(leetcode)