mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #500 from jackeyjia/patch-19
add js solution for longestPalindromeSubseq
This commit is contained in:
@ -214,7 +214,29 @@ func longestPalindromeSubseq(s string) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```javascript
|
||||
const longestPalindromeSubseq = (s) => {
|
||||
const strLen = s.length;
|
||||
let dp = Array.from(Array(strLen), () => Array(strLen).fill(0));
|
||||
|
||||
for(let i = 0; i < strLen; i++) {
|
||||
dp[i][i] = 1;
|
||||
}
|
||||
|
||||
for(let i = strLen - 1; i >= 0; i--) {
|
||||
for(let j = i + 1; j < strLen; j++) {
|
||||
if(s[i] === s[j]) {
|
||||
dp[i][j] = dp[i+1][j-1] + 2;
|
||||
} else {
|
||||
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[0][strLen - 1];
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user