R8-多维dp篇
直接上递推
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
char[] s=text1.toCharArray();
char[] t=text2.toCharArray();
int n=s.length;
int m=t.length;
int [][] f=new int[n+1][m+1];
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
f[i+1][j+1]=s[i]==t[j]?f[i][j]+1:Math.max(f[i][j+1],f[i+1][j]);
}
}
return f[n][m];
}
}
ps:
java语法
char[] s = text1.toCharArray();