class Solution {
public int longestPalindromeSubseq(String s) {
int len = s.length();
//dp[i][j]:字符串s在[i, j]范围内最长的回文子序列的长度为dp[i][j]
int[][] dp = new int[len+1][len+1];//默认值为0
for(int i = len - 1; i >= 0; i--) {//从后往前遍历 保证情况不漏
dp[i][i] = 1;//初始化
for(int j = i + 1; j < len; j++) {
if(s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i+1][j-1] + 2;
}else {
dp[i][j] = Math.max(dp[i + 1][j], Math.max(dp[i][j], dp[i][j - 1]));
}
}
}
return dp[0][len-1];
}
}
05-13
602
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-14
692
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-03
827
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-02
1190
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)