File tree Expand file tree Collapse file tree 6 files changed +142
-0
lines changed Expand file tree Collapse file tree 6 files changed +142
-0
lines changed Original file line number Diff line number Diff line change 32
32
104. 二叉树的最大深度
33
33
105. 从前序与中序遍历序列构造二叉树
34
34
114. 二叉树展开为链表
35
+ 115. 不同的子序列
35
36
121. 买卖股票的最佳时机
36
37
122. 买卖股票的最佳时机 II
37
38
123. 买卖股票的最佳时机 III
61
62
309. 最佳买卖股票时机含冷冻期
62
63
322. 零钱兑换
63
64
338. 比特位计数
65
+ 392. 判断子序列
64
66
406. 根据身高重建队列
65
67
416. 分割等和子集
66
68
437. 路径总和
71
73
491. 递增子序列
72
74
494. 目标和
73
75
509. 斐波那契数
76
+ 516. 最长回文子序列
74
77
518. 零钱兑换 II
75
78
538. 把二叉搜索树转换为累加树
76
79
543. 二叉树的直径
77
80
560. 和为 K 的子数组
78
81
583. 两个字符串的删除操作
79
82
589. N叉树的前序遍历
80
83
617. 合并二叉树
84
+ 647. 回文子串
81
85
674. 最长连续递增序列
82
86
695. 岛屿的最大面积
83
87
698. 划分为k个相等的子集
84
88
712. 两个字符串的最小ASCII删除和
85
89
714. 买卖股票的最佳时机含手续费
86
90
718. 最长重复子数组
87
91
1020. 飞地的数量
92
+ 1035. 不相交的线
88
93
1143. 最长公共子序列
89
94
1254. 统计封闭岛屿的数目
90
95
1905. 统计子岛屿
Original file line number Diff line number Diff line change
1
+ // 115. 不同的子序列
2
+
3
+
4
+ class Solution {
5
+ public int numDistinct (String s , String t ) {
6
+ int n = s .length (), m = t .length ();
7
+ int [][] dp = new int [n + 1 ][m + 1 ];
8
+ for (int i = 0 ; i <= n ; i ++) {
9
+ dp [i ][0 ] = 1 ;
10
+ }
11
+ for (int i = 1 ; i <= n ; i ++) {
12
+ for (int j = 1 ; j <= m ; j ++) {
13
+ if (s .charAt (i - 1 ) == t .charAt (j - 1 )) {
14
+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + dp [i - 1 ][j ];
15
+ } else {
16
+ dp [i ][j ] = dp [i - 1 ][j ];
17
+ }
18
+ }
19
+ }
20
+ return dp [n ][m ];
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ // 392. 判断子序列
2
+
3
+
4
+ /*
5
+ 实际就是“1143.最长公共子序列”
6
+ */
7
+ class Solution {
8
+ public boolean isSubsequence (String s , String t ) {
9
+ int n = s .length (), m = t .length ();
10
+ int [][] dp = new int [n + 1 ][m + 1 ];
11
+ for (int i = 1 ; i <= n ; i ++) {
12
+ for (int j = 1 ; j <= m ; j ++) {
13
+ if (s .charAt (i - 1 ) == t .charAt (j - 1 )) {
14
+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1 ;
15
+ } else {
16
+ dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
17
+ }
18
+ }
19
+ }
20
+ return dp [n ][m ] == n ? true : false ;
21
+ }
22
+ }
23
+
24
+
25
+ /*
26
+ 双指针:两个指针分别指向字符串s和t,当两个指针指向的字符相等时 i指针才向右移动一步,j指针每次都向右移动一步,只要其中一个字符串遍历完就结束,
27
+ 最后判断i指针是否到了末尾,是则表示s是t的子序列,否则不是
28
+ */
29
+ class Solution {
30
+ public boolean isSubsequence (String s , String t ) {
31
+ int n = s .length (), m = t .length ();
32
+ int i = 0 , j = 0 ;
33
+ while (i < n && j < m ) {
34
+ if (s .charAt (i ) == t .charAt (j )) {
35
+ i ++;
36
+ }
37
+ j ++;
38
+ }
39
+ return i == n ? true : false ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ // 516. 最长回文子序列
2
+
3
+
4
+ class Solution {
5
+ public int longestPalindromeSubseq (String s ) {
6
+ int n = s .length (), res = 1 ;
7
+ int [][] dp = new int [n ][n ];
8
+ for (int i = 0 ; i < n ; i ++) {
9
+ dp [i ][i ] = 1 ;
10
+ }
11
+ for (int r = 1 ; r < n ; r ++) {
12
+ for (int l = r - 1 ; l >= 0 ; l --) {
13
+ if (s .charAt (l ) == s .charAt (r )) {
14
+ if (r - l == 1 ) {
15
+ dp [l ][r ] = 2 ;
16
+ } else {
17
+ dp [l ][r ] = dp [l + 1 ][r - 1 ] + 2 ;
18
+ }
19
+ } else {
20
+ dp [l ][r ] = Math .max (dp [l + 1 ][r ], dp [l ][r - 1 ]);
21
+ }
22
+ res = Math .max (res , dp [l ][r ]);
23
+ }
24
+ }
25
+ return res ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ // 647. 回文子串
2
+
3
+
4
+ /*
5
+ 与“5.最长回文子串”相似
6
+ */
7
+ class Solution {
8
+ public int countSubstrings (String s ) {
9
+ int n = s .length ();
10
+ int res = n ;
11
+ boolean [][] dp = new boolean [n ][n ];
12
+ for (int i = 0 ; i < n ; i ++) {
13
+ dp [i ][i ] = true ;
14
+ }
15
+ for (int r = 1 ; r < n ; r ++) {
16
+ for (int l = 0 ; l < r ; l ++) {
17
+ if (s .charAt (l ) == s .charAt (r ) && (r - l < 3 || dp [l + 1 ][r - 1 ])) {
18
+ dp [l ][r ] = true ;
19
+ res ++;
20
+ }
21
+ }
22
+ }
23
+ return res ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ // 1035. 不相交的线
2
+
3
+
4
+ /*
5
+ 实际就是“1143.最长公共子序列”
6
+ */
7
+ class Solution {
8
+ public int maxUncrossedLines (int [] nums1 , int [] nums2 ) {
9
+ int n = nums1 .length , m = nums2 .length ;
10
+ int [][] dp = new int [n + 1 ][m + 1 ];
11
+ for (int i = 1 ; i <= n ; i ++) {
12
+ for (int j = 1 ; j <= m ; j ++) {
13
+ if (nums1 [i - 1 ] == nums2 [j - 1 ]) {
14
+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1 ;
15
+ } else {
16
+ dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
17
+ }
18
+ }
19
+ }
20
+ return dp [n ][m ];
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments