Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit80dda7a

Browse files
authored
add js solution for longestPalindromeSubseq
1 parente70378f commit80dda7a

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+
constlongestPalindromeSubseq= (s)=> {
220+
conststrLen=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)

[8]ページ先頭

©2009-2025 Movatter.jp