[抄题]:
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Example 1:
Input:
"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
for i = s.length() - 1; i >= 都可以成为找bug的对象
[思维问题]:
忘记回文串dp怎么写了。而且这道题自称坐标型 && index顺序略微奇葩。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
dp回文串就是:加2或者取两者中较大值
[复杂度]:Time complexity: O(方) Space complexity: O(方)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int longestPalindromeSubseq(String s) {
//cc
if (s == null || s.length() == 0) return 0; //ini n, dp[][] == 1
int n = s.length();
int[][] dp = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
//ini
dp[i][i] = 1;
for (int j = i + 1; j < n; j++) {
//discuss in 2 cases: dp[i][j] = dp[i + 1][j - 1] + 2 or max(dp[i][j - 1], dp[i + 1][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][j - 1], dp[i + 1][j]);
}
}
} return dp[0][n - 1];
}
}