java刷题--1143最长公共子序列
题目
代码
class Solution {
public int longestCommonSubsequence(String s1, String s2) {
int[][] dp = new int[s1.length() + 1][s2.length() + 1];
for (int i = 0; i < s1.length(); ++i)
for (int j = 0; j < s2.length(); ++j)
if (s1.charAt(i) == s2.charAt(j)) dp[i + 1][j + 1] = 1 + dp[i][j];
else dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j]);
return dp[s1.length()][s2.length()];
}
}