Skip to content

Commit cdc3cf0

Browse files
committed
二刷62
1 parent d2c23e9 commit cdc3cf0

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

docs/0000-26-dynamic-programming.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ image::images/dynamic-programming-01.jpeg[{image_attr}]
77

88
:sectnums:
99

10+
动态规划的五部曲:
11+
12+
. 确定 dp 数组(dp table)以及下标的含义
13+
. 确定递推公式
14+
. dp 数组如何初始化
15+
. 确定遍历顺序
16+
. 举例推导 dp 数组
17+
1018
include::0000-26-dp-1-0-1-knapsack.adoc[leveloffset=+1]
1119

1220
include::0000-26-dp-2-unbounded-knapsack.adoc[leveloffset=+1]

docs/0062-unique-paths.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,34 @@ From the top-left corner, there are a total of 3 ways to reach the bottom-right
3838
*Output:* 28
3939
----
4040

41+
== 思路分析
4142

4243
[[src-0062]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
4349
[{java_src_attr}]
4450
----
4551
include::{sourcedir}/_0062_UniquePaths.java[tag=answer]
4652
----
53+
--
54+
55+
二刷::
56+
+
57+
--
58+
[{java_src_attr}]
59+
----
60+
include::{sourcedir}/_0062_UniquePaths_2.java[tag=answer]
61+
----
62+
--
63+
====
64+
65+
== 参考资料
66+
67+
. https://leetcode.cn/problems/unique-paths/solutions/5772/dong-tai-gui-hua-by-powcai-2/[62. 不同路径 - 力扣(LeetCode)^]
68+
. https://leetcode.cn/problems/unique-paths/solutions/514311/bu-tong-lu-jing-by-leetcode-solution-hzjf/[62. 不同路径 - 官方题解^]
69+
70+
4771

logbook/202406.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@
454454
|{doc_base_url}/0746-min-cost-climbing-stairs.adoc[题解]
455455
|动态规划
456456

457+
|{counter:codes}
458+
|{leetcode_base_url}/unique-paths/[62. Unique Paths^]
459+
|{doc_base_url}/0062-unique-paths.adoc[题解]
460+
|动态规划
457461

458462
|===
459463

src/main/java/com/diguage/algo/leetcode/_0062_UniquePaths.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
*/
4040
public class _0062_UniquePaths {
4141
// tag::answer[]
42+
43+
/**
44+
* @author D瓜哥 · https://www.diguage.com
45+
* @since 2019-10-26 22:56
46+
*/
4247
public int uniquePaths(int m, int n) {
4348
if (m == 0 || n == 0) {
4449
return 0;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0062_UniquePaths_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-12 15:16:31
8+
*/
9+
public int uniquePaths(int m, int n) {
10+
// 1. dp 表示走到某个节点一共有多少路径
11+
int[][] dp = new int[m][n];
12+
// 3. 第一行只有一种走法
13+
for (int i = 0; i < m; i++) {
14+
dp[i][0] = 1;
15+
}
16+
// 3. 第一列也只有一种走法。
17+
for (int i = 0; i < n; i++) {
18+
dp[0][i] = 1;
19+
}
20+
// 4. 遍历顺序:从左上逐步向右下遍历
21+
for (int i = 1; i < m; i++) {
22+
for (int j = 1; j < n; j++) {
23+
// 2. 由于只能从上或者从左走过来,
24+
// 那么路径数量就是两个节点相加
25+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
26+
}
27+
}
28+
return dp[m - 1][n - 1];
29+
}
30+
// end::answer[]
31+
}

0 commit comments

Comments
 (0)