Skip to content

Commit dc531fd

Browse files
Merge pull request youngyangyang04#500 from jackeyjia/patch-19
add js solution for longestPalindromeSubseq
2 parents 9555e08 + 80dda7a commit dc531fd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

problems/0516.最长回文子序列.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,29 @@ func longestPalindromeSubseq(s string) int {
214214
}
215215
```
216216

217+
Javascript:
218+
```javascript
219+
const longestPalindromeSubseq = (s) => {
220+
const strLen = s.length;
221+
let dp = Array.from(Array(strLen), () => Array(strLen).fill(0));
222+
223+
for(let i = 0; i < strLen; i++) {
224+
dp[i][i] = 1;
225+
}
217226

227+
for(let i = strLen - 1; i >= 0; i--) {
228+
for(let j = i + 1; j < strLen; j++) {
229+
if(s[i] === s[j]) {
230+
dp[i][j] = dp[i+1][j-1] + 2;
231+
} else {
232+
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
233+
}
234+
}
235+
}
236+
237+
return dp[0][strLen - 1];
238+
};
239+
```
218240

219241

220242
-----------------------

0 commit comments

Comments
 (0)