Skip to content

Commit 331e3c7

Browse files
committed
二刷63
1 parent cdc3cf0 commit 331e3c7

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

docs/0063-unique-paths-ii.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,34 @@ There are two ways to reach the bottom-right corner:
3333
2. Down -> Down -> Right -> Right
3434
----
3535

36+
== 思路分析
37+
38+
image::images/0063-01.png[{image_attr}]
3639

3740
[[src-0063]]
41+
[tabs]
42+
====
43+
一刷::
44+
+
45+
--
3846
[{java_src_attr}]
3947
----
4048
include::{sourcedir}/_0063_UniquePathsII.java[tag=answer]
4149
----
50+
--
51+
52+
二刷::
53+
+
54+
--
55+
[{java_src_attr}]
56+
----
57+
include::{sourcedir}/_0063_UniquePathsII_2.java[tag=answer]
58+
----
59+
--
60+
====
61+
62+
== 参考资料
63+
64+
. https://leetcode.cn/problems/unique-paths-ii/solutions/316968/bu-tong-lu-jing-ii-by-leetcode-solution-2/[63. 不同路径 II - 官方题解^]
65+
. https://leetcode.cn/problems/unique-paths-ii/solutions/2732049/javapython3cdong-tai-gui-hua-kong-jian-y-nyjy/[63. 不同路径 II - 动态规划 + 空间优化^]
4266

docs/images/0063-01.png

124 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@
459459
|{doc_base_url}/0062-unique-paths.adoc[题解]
460460
|动态规划
461461

462+
|{counter:codes}
463+
|{leetcode_base_url}/unique-paths-ii/[63. Unique Paths II^]
464+
|{doc_base_url}/0063-unique-paths-ii.adoc[题解]
465+
|动态规划
466+
462467
|===
463468

464469
截止目前,本轮练习一共完成 {codes} 道题。

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class _0063_UniquePathsII {
4343
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Paths II.
4444
* <p>
4545
* Memory Usage: 40.5 MB, less than 33.84% of Java online submissions for Unique Paths II.
46+
*
47+
* @author D瓜哥 · https://www.diguage.com
48+
* @since 2019-10-26 23:50
4649
*/
4750
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
4851
if (Objects.isNull(obstacleGrid)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import static com.diguage.util.Printers.printMatrix;
4+
5+
public class _0063_UniquePathsII_2 {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2019-10-26 23:50
11+
*/
12+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
13+
int row = obstacleGrid.length;
14+
int column = obstacleGrid[0].length;
15+
// 1. obstacleGrid 表示走到某个节点一共有多少路径
16+
// 将障碍设置为-1,便于和 0 区分
17+
for (int i = 0; i < row; i++) {
18+
for (int j = 0; j < column; j++) {
19+
if (obstacleGrid[i][j] == 1) {
20+
obstacleGrid[i][j] = -1;
21+
}
22+
}
23+
}
24+
// 3. 第一行也只有一种走法,如果有障碍,则后面的路径为0
25+
for (int i = 0; i < column; i++) {
26+
if (obstacleGrid[0][i] == -1) {
27+
break;
28+
}
29+
obstacleGrid[0][i] = 1;
30+
}
31+
// 3. 第一列只有一种走法,如果有障碍,则后面的路径为0
32+
for (int i = 0; i < row; i++) {
33+
if (obstacleGrid[i][0] == -1) {
34+
break;
35+
}
36+
obstacleGrid[i][0] = 1;
37+
}
38+
printMatrix(obstacleGrid);
39+
// 4. 确定遍历顺序:从左上向右下遍历
40+
for (int i = 1; i < row; i++) {
41+
for (int j = 1; j < column; j++) {
42+
if (obstacleGrid[i][j] == -1) {
43+
continue;
44+
}
45+
// 2. 确定递推公式
46+
int mi = 0;
47+
if (obstacleGrid[i - 1][j] != -1) {
48+
mi = obstacleGrid[i - 1][j];
49+
}
50+
int ni = 0;
51+
if (obstacleGrid[i][j - 1] != -1) {
52+
ni = obstacleGrid[i][j - 1];
53+
}
54+
obstacleGrid[i][j] = mi + ni;
55+
printMatrix(obstacleGrid);
56+
}
57+
}
58+
return obstacleGrid[row - 1][column - 1] == -1 ? 0 : obstacleGrid[row - 1][column - 1];
59+
}
60+
61+
// end::answer[]
62+
public static void main(String[] args) {
63+
int[][] obstacleGrid = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
64+
new _0063_UniquePathsII_2().uniquePathsWithObstacles(obstacleGrid);
65+
}
66+
}

0 commit comments

Comments
 (0)