国际站
class Solution {
static public int longestCommonSubsequence(String text1, String text2) {
//确定边界
if( text1 == null || text2 == null){
return 0;
}
int m = text1.length();
int n = text2.length();
//确定dp状态数组 +1只是为了好处理
int [][] dp = new int[m+1][n+1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
//检查是否相等,如果相等则取左上角的数+1
if(text1.charAt(i-1) == text2.charAt(j-1)){
dp[i][j] = dp[i-1][j-1] + 1;
}else{
//否则就取两个角的最大值
dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
}
}
}
return dp[m][n];
}
}