Skip to content

Commit b75f232

Browse files
committed
字符串的动态规划系列
1 parent 95d6b14 commit b75f232

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

leetcode_Java/DoneTitle.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
104. 二叉树的最大深度
3333
105. 从前序与中序遍历序列构造二叉树
3434
114. 二叉树展开为链表
35+
115. 不同的子序列
3536
121. 买卖股票的最佳时机
3637
122. 买卖股票的最佳时机 II
3738
123. 买卖股票的最佳时机 III
@@ -61,6 +62,7 @@
6162
309. 最佳买卖股票时机含冷冻期
6263
322. 零钱兑换
6364
338. 比特位计数
65+
392. 判断子序列
6466
406. 根据身高重建队列
6567
416. 分割等和子集
6668
437. 路径总和
@@ -71,20 +73,23 @@
7173
491. 递增子序列
7274
494. 目标和
7375
509. 斐波那契数
76+
516. 最长回文子序列
7477
518. 零钱兑换 II
7578
538. 把二叉搜索树转换为累加树
7679
543. 二叉树的直径
7780
560. 和为 K 的子数组
7881
583. 两个字符串的删除操作
7982
589. N叉树的前序遍历
8083
617. 合并二叉树
84+
647. 回文子串
8185
674. 最长连续递增序列
8286
695. 岛屿的最大面积
8387
698. 划分为k个相等的子集
8488
712. 两个字符串的最小ASCII删除和
8589
714. 买卖股票的最佳时机含手续费
8690
718. 最长重复子数组
8791
1020. 飞地的数量
92+
1035. 不相交的线
8893
1143. 最长公共子序列
8994
1254. 统计封闭岛屿的数目
9095
1905. 统计子岛屿

leetcode_Java/Solution0115.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

leetcode_Java/Solution0392.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
}

leetcode_Java/Solution0516.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

leetcode_Java/Solution0647.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

leetcode_Java/Solution1035.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)