File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,35 @@ const longestPalindromeSubseq = (s) => {
236
236
};
237
237
```
238
238
239
+ TypeScript:
240
+
241
+ ``` typescript
242
+ function longestPalindromeSubseq(s : string ): number {
243
+ /**
244
+ dp[i][j]:[i,j]区间内,最长回文子序列的长度
245
+ */
246
+ const length: number = s .length ;
247
+ const dp: number [][] = new Array (length ).fill (0 )
248
+ .map (_ => new Array (length ).fill (0 ));
249
+ for (let i = 0 ; i < length ; i ++ ) {
250
+ dp [i ][i ] = 1 ;
251
+ }
252
+ // 自下而上,自左往右遍历
253
+ for (let i = length - 1 ; i >= 0 ; i -- ) {
254
+ for (let j = i + 1 ; j < length ; j ++ ) {
255
+ if (s [i ] === s [j ]) {
256
+ dp [i ][j ] = dp [i + 1 ][j - 1 ] + 2 ;
257
+ } else {
258
+ dp [i ][j ] = Math .max (dp [i ][j - 1 ], dp [i + 1 ][j ]);
259
+ }
260
+ }
261
+ }
262
+ return dp [0 ][length - 1 ];
263
+ };
264
+ ```
265
+
266
+
267
+
239
268
240
269
-----------------------
241
270
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img ></div >
You can’t perform that action at this time.
0 commit comments