File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
longest-palindromic-subsequence Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ Step 2. Add the dependency
49
49
50
50
<summary >展开查看</summary >
51
51
52
+ https://leetcode-cn.com/problems/longest-palindromic-subsequence/
53
+
52
54
https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/
53
55
54
56
https://leetcode.cn/problems/palindromic-substrings/
Original file line number Diff line number Diff line change
1
+ function longestPalindromeSubseq ( s : string ) : number {
2
+ const n = s . length ;
3
+ const dp : number [ ] [ ] = new Array ( n ) . fill ( 0 ) . map ( ( ) => new Array ( n ) . fill ( 0 ) ) ;
4
+ for ( let i = n - 1 ; i >= 0 ; i -- ) {
5
+ dp [ i ] [ i ] = 1 ;
6
+ const c1 = s [ i ] ;
7
+ for ( let j = i + 1 ; j < n ; j ++ ) {
8
+ const c2 = s [ j ] ;
9
+ if ( c1 === c2 ) {
10
+ dp [ i ] [ j ] = dp [ i + 1 ] [ j - 1 ] + 2 ;
11
+ } else {
12
+ dp [ i ] [ j ] = Math . max ( dp [ i + 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ;
13
+ }
14
+ }
15
+ }
16
+ return dp [ 0 ] [ n - 1 ] ;
17
+ }
18
+
19
+ export default longestPalindromeSubseq ;
You can’t perform that action at this time.
0 commit comments