没做过想不出来这样的题目居然是二维数组dp
/**
* 二维数组动态规划
* Runtime: 5 ms, faster than 99.52%
* Memory Usage: 42.5 MB, less than 72.90%
*/
class Solution {
public int longestCommonSubsequence(String text1, String text2) {
char[] text1Arr = text1.toCharArray(), text2Arr = text2.toCharArray();
int[][] dp = new int[text1Arr.length + 1][text2Arr.length + 1];
for (int i = 1; i <= text1Arr.length; i++) {
for (int j = 1; j <= text2Arr.length; j++) {
dp[i][j] = text1Arr[i - 1] == text2Arr[j - 1] ? dp[i - 1][j - 1] + 1 : Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
return dp[text1Arr.length][text2Arr.length];
}
}