File tree Expand file tree Collapse file tree 5 files changed +61
-8
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 5 files changed +61
-8
lines changed Original file line number Diff line number Diff line change 11[#0064-minimum-path-sum]
22= 64. 最小路径和
33
4- https://leetcode.cn/problems/minimum-path-sum/[LeetCode - 64. 最小路径和 ^]
4+ https://leetcode.cn/problems/minimum-path-sum/[LeetCode - 64. 最小路径和^]
55
66给定一个包含非负整数的 `m x n` 网格 `grid` ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
77
@@ -26,14 +26,22 @@ image::images/0064-01.jpg[{image_attr}]
2626
2727*提示:*
2828
29- * `+ m == grid.length+ `
30- * `+ n == grid[i].length+ `
31- * `+1 <= m, n <= 200+ `
32- * `+0 <= grid[i][j] <= 200+ `
29+ * `m == grid.length`
30+ * `n == grid[i].length`
31+ * `1 \ <= m, n \ <= 200`
32+ * `0 \ <= grid[i][j] \ <= 200`
3333
3434
3535== 思路分析
3636
37+ 动态规划:
38+
39+ . 第一行只能从左向右,直接依次向后累计即可。
40+ . 第一列也是只能从上向下,直接向下累加即可。
41+ . 其余只能从左或上到当前格子,两个格子取最小累加即可。
42+
43+ image::images/0064-10.png[{image_attr}]
44+
3745[[src-0064]]
3846[tabs]
3947====
@@ -63,11 +71,22 @@ include::{sourcedir}/_0064_MinimumPathSum_2.java[tag=answer]
6371include::{sourcedir}/_0064_MinimumPathSum_3.java[tag=answer]
6472----
6573--
74+
75+ 四刷::
76+ +
77+ --
78+ [{java_src_attr}]
79+ ----
80+ include::{sourcedir}/_0064_MinimumPathSum_4.java[tag=answer]
81+ ----
82+ --
6683====
6784
6885== 参考资料
6986
70- . https://leetcode.cn/problems/minimum-path-sum/solutions/342122/zui-xiao-lu-jing-he-by-leetcode-solution/?envType=study-plan-v2&envId=selected-coding-interview[64. 最小路径和 - 官方题解^]
71- . https://leetcode.cn/problems/minimum-path-sum/solutions/25943/zui-xiao-lu-jing-he-dong-tai-gui-hua-gui-fan-liu-c/?envType=study-plan-v2&envId=selected-coding-interview[64. 最小路径和 - 动态规划,规范流程,清晰图解^]
87+ . https://leetcode.cn/problems/minimum-path-sum/solutions/3045828/jiao-ni-yi-bu-bu-si-kao-dpcong-ji-yi-hua-zfb2/[64. 最小路径和 - 教你一步步思考 DP:从记忆化搜索到递推到空间优化!^]
88+ . https://leetcode.cn/problems/minimum-path-sum/solutions/25943/zui-xiao-lu-jing-he-dong-tai-gui-hua-gui-fan-liu-c/[64. 最小路径和 - 动态规划,规范流程,清晰图解^]
89+ . https://leetcode.cn/problems/minimum-path-sum/solutions/342122/zui-xiao-lu-jing-he-by-leetcode-solution/[64. 最小路径和 - 官方题解^]
90+ . https://leetcode.cn/problems/minimum-path-sum/solutions/2728404/javapython3cdong-tai-gui-hua-kong-jian-y-a5sz/[64. 最小路径和 - 动态规划+空间优化:一步步进行代码优化【图解】^]
7291
7392
Original file line number Diff line number Diff line change @@ -1959,6 +1959,11 @@ endif::[]
19591959|{doc_base_url} /0070-climbing-stairs.adoc[题解]
19601960|✅ 动态规划。也就是斐波那契数列,没想到这竟然是最简单的动态规划题目。
19611961
1962+ |{counter:codes2503}
1963+ |{leetcode_base_url} /minimum-path-sum/[64. 最小路径和^]
1964+ |{doc_base_url} /0064-minimum-path-sum.adoc[题解]
1965+ |✅ 动态规划。每格只能从上或左到此,所以,在“上”和“左”中取最小,累加即可。直接在原有矩阵上操作,更加节省空间。
1966+
19621967|===
19631968
19641969截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change 1+ package com .diguage .algo .leetcode ;
2+
3+ public class _0064_MinimumPathSum_4 {
4+ // tag::answer[]
5+
6+ /**
7+ * @author D瓜哥 · https://www.diguage.com
8+ * @since 2025-11-21 23:25:37
9+ */
10+ public int minPathSum (int [][] grid ) {
11+ int row = grid .length ;
12+ int col = grid [0 ].length ;
13+ for (int c = 1 ; c < col ; c ++) {
14+ grid [0 ][c ] += grid [0 ][c - 1 ];
15+ }
16+ for (int r = 1 ; r < row ; r ++) {
17+ grid [r ][0 ] += grid [r - 1 ][0 ];
18+ for (int c = 1 ; c < col ; c ++) {
19+ grid [r ][c ] += Math .min (grid [r - 1 ][c ], grid [r ][c - 1 ]);
20+ }
21+ }
22+ return grid [row - 1 ][col - 1 ];
23+ }
24+ // end::answer[]
25+ }
Original file line number Diff line number Diff line change 3636 * @since 2018-07-14 15:30
3737 */
3838public class _0066_PlusOne {
39- // tag::answer[]
39+ // tag::answer[]
40+ /**
41+ * @author D瓜哥 · https://www.diguage.com
42+ * @since 2018-07-14 15:30
43+ */
4044 public static int [] plusOne (int [] digits ) {
4145 if (digits == null || digits .length == 0 ) {
4246 return digits ;
You can’t perform that action at this time.
0 commit comments