We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9a056ed commit 514d728Copy full SHA for 514d728
1 file changed
leetcode/516.cpp
@@ -0,0 +1,22 @@
1
+class Solution {
2
+public:
3
+ int longestPalindromeSubseq(string s) {
4
+ int dp[1001][1001] = {0};
5
+ // dp[i][j] = the lps of s[i..j]
6
+
7
+ for (int i = 0; i <= s.size(); ++i) {
8
+ dp[i][i] = 1;
9
+ }
10
11
+ for (int j = 2; j <= s.size(); ++j) {
12
+ for (int i = j-1; i > 0; --i) {
13
+ if (s[i-1] == s[j-1]) {
14
+ dp[i][j] = dp[i+1][j-1] + 2;
15
+ } else {
16
+ dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
17
18
19
20
+ return dp[1][s.size()];
21
22
+};
0 commit comments