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];
}
};
No.53 - leetCode1143 - 最长公共子序列
最新推荐文章于 2023-10-15 12:39:46 发布