Skip to content

Commit 992b1d7

Browse files
committed
modify code
1 parent 190e3b1 commit 992b1d7

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

src/class067/Code01_MinimumPathSum.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@
77
// 测试链接 : https://leetcode.cn/problems/minimum-path-sum/
88
public class Code01_MinimumPathSum {
99

10+
// 暴力递归
1011
public static int minPathSum1(int[][] grid) {
1112
return f1(grid, grid.length - 1, grid[0].length - 1);
1213
}
1314

15+
// 从(0,0)到(i,j)最小路径和
16+
// 一定每次只能向右或者向下
1417
public static int f1(int[][] grid, int i, int j) {
1518
if (i == 0 && j == 0) {
1619
return grid[0][0];
1720
}
1821
int up = Integer.MAX_VALUE;
1922
int left = Integer.MAX_VALUE;
20-
if (i > 0) {
23+
if (i - 1 >= 0) {
2124
up = f1(grid, i - 1, j);
2225
}
23-
if (j > 0) {
26+
if (j - 1 >= 0) {
2427
left = f1(grid, i, j - 1);
2528
}
2629
return grid[i][j] + Math.min(up, left);
2730
}
2831

32+
// 记忆化搜索
2933
public static int minPathSum2(int[][] grid) {
3034
int n = grid.length;
3135
int m = grid[0].length;
@@ -48,10 +52,10 @@ public static int f2(int[][] grid, int i, int j, int[][] dp) {
4852
} else {
4953
int up = Integer.MAX_VALUE;
5054
int left = Integer.MAX_VALUE;
51-
if (i > 0) {
55+
if (i - 1 >= 0) {
5256
up = f2(grid, i - 1, j, dp);
5357
}
54-
if (j > 0) {
58+
if (j - 1 >= 0) {
5559
left = f2(grid, i, j - 1, dp);
5660
}
5761
ans = grid[i][j] + Math.min(up, left);
@@ -60,6 +64,7 @@ public static int f2(int[][] grid, int i, int j, int[][] dp) {
6064
return ans;
6165
}
6266

67+
// 严格位置依赖的动态规划
6368
public static int minPathSum3(int[][] grid) {
6469
int n = grid.length;
6570
int m = grid[0].length;
@@ -79,15 +84,22 @@ public static int minPathSum3(int[][] grid) {
7984
return dp[n - 1][m - 1];
8085
}
8186

87+
// 严格位置依赖的动态规划 + 空间压缩技巧
8288
public static int minPathSum4(int[][] grid) {
8389
int n = grid.length;
8490
int m = grid[0].length;
91+
// 先让dp表,变成想象中的表的第0行的数据
8592
int[] dp = new int[m];
8693
dp[0] = grid[0][0];
8794
for (int j = 1; j < m; j++) {
8895
dp[j] = dp[j - 1] + grid[0][j];
8996
}
9097
for (int i = 1; i < n; i++) {
98+
// i = 1,dp表变成想象中二维表的第1行的数据
99+
// i = 2,dp表变成想象中二维表的第2行的数据
100+
// i = 3,dp表变成想象中二维表的第3行的数据
101+
// ...
102+
// i = n-1,dp表变成想象中二维表的第n-1行的数据
91103
dp[0] += grid[i][0];
92104
for (int j = 1; j < m; j++) {
93105
dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];

src/class067/Code04_LongestCommonSubsequence.java renamed to src/class067/Code03_LongestCommonSubsequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// 如果不存在公共子序列,返回0
77
// 两个字符串的 公共子序列 是这两个字符串所共同拥有的子序列
88
// 测试链接 : https://leetcode.cn/problems/longest-common-subsequence/
9-
public class Code04_LongestCommonSubsequence {
9+
public class Code03_LongestCommonSubsequence {
1010

1111
public static int longestCommonSubsequence1(String str1, String str2) {
1212
char[] s1 = str1.toCharArray();

src/class067/Code05_LongestPalindromicSubsequence.java renamed to src/class067/Code04_LongestPalindromicSubsequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// 最长回文子序列
44
// 给你一个字符串 s ,找出其中最长的回文子序列,并返回该序列的长度
55
// 测试链接 : https://leetcode.cn/problems/longest-palindromic-subsequence/
6-
public class Code05_LongestPalindromicSubsequence {
6+
public class Code04_LongestPalindromicSubsequence {
77

88
// 最长回文子序列问题可以转化成最长公共子序列问题
99
// 不过这里讲述区间动态规划的思路

src/class067/Code06_NodenHeightNotLargerThanm.java renamed to src/class067/Code05_NodenHeightNotLargerThanm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.io.PrintWriter;
1717
import java.io.StreamTokenizer;
1818

19-
public class Code06_NodenHeightNotLargerThanm {
19+
public class Code05_NodenHeightNotLargerThanm {
2020

2121
public static void main(String[] args) throws IOException {
2222
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

src/class067/Code03_LongestIncreasingPath.java renamed to src/class067/Code06_LongestIncreasingPath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// 对于每个单元格,你可以往上,下,左,右四个方向移动
66
// 你 不能 在 对角线 方向上移动或移动到 边界外(即不允许环绕)
77
// 测试链接 : https://leetcode.cn/problems/longest-increasing-path-in-a-matrix/
8-
public class Code03_LongestIncreasingPath {
8+
public class Code06_LongestIncreasingPath {
99

1010
public static int longestIncreasingPath1(int[][] grid) {
1111
int ans = 0;

0 commit comments

Comments
 (0)