Skip to content

Commit 514d728

Browse files
committed
add leetcode 516.cpp
1 parent 9a056ed commit 514d728

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

leetcode/516.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)