Skip to content

Commit 81ed96d

Browse files
committed
add 0063
1 parent 845a0df commit 81ed96d

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
| 49 | [Group Anagrams][0049] | Hash Table, String |
8686
| 50 | [Pow(x, n)][0050] | Math, Binary Search |
8787
| 56 | [Merge Intervals][0056] | Array, Sort |
88+
| 63 | [不同路径 II(Unique Paths II)][0063] | 数组、动态规划 |
8889
| 209 | [长度最小的子数组(Minimum Size Subarray Sum)][0209] | 数组、双指针、二分查找 |
8990
| 215 | [数组中的第K个最大元素(Kth Largest Element in an Array)][0215] | 堆、分治算法 |
9091
| 554 | [Brick Wall][0554] | Hash Table |
@@ -169,6 +170,7 @@
169170
[0049]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0049/README.md
170171
[0050]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0050/README.md
171172
[0056]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0056/README.md
173+
[0063]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0063/README.md
172174
[0209]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0209/README.md
173175
[0215]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0215/README.md
174176
[0554]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/0554/README.md

note/0063/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [不同路径 II(Unique Paths II)][title]
2+
3+
## 题目描述
4+
5+
一个机器人位于一个 _m x n_ 网格的左上角 (起始点在下图中标记为“Start” )。
6+
7+
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
8+
9+
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
10+
11+
![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/22/robot_maze.png)
12+
13+
网格中的障碍物和空位置分别用 `1``0` 来表示。
14+
15+
**说明:**_m_ 和 _n_ 的值均不超过 100。
16+
17+
**示例 1:**
18+
```
19+
输入:
20+
[
21+
  [0,0,0],
22+
  [0,1,0],
23+
  [0,0,0]
24+
]
25+
输出: 2
26+
解释:
27+
3x3 网格的正中间有一个障碍物。
28+
从左上角到右下角一共有 2 条不同的路径:
29+
1. 向右 -> 向右 -> 向下 -> 向下
30+
2. 向下 -> 向下 -> 向右 -> 向右
31+
```
32+
33+
**标签:** 数组、动态规划
34+
35+
36+
## 思路
37+
38+
做过爬楼梯的应该很快就能想到这是一道很典型的动态规划题目,
39+
40+
我们令 `dp[i][j]` 表示走到格子 `(i, j)` 的路径数,
41+
42+
那么当 `(i, j)` 没障碍物时,`dp[i][j] = 0`
43+
44+
那么当 `(i, j)` 有障碍物时,`dp[i][j] = dp[i - 1][j] + dp[i][j - 1]`
45+
46+
其初始态第 1 列(行)的格子只有从其上(左)边格子走过去这一种走法,因此初始化 `dp[i][0]``dp[0][j]`)值为 1,且遇到障碍物时后面值都为 0;
47+
48+
有了这些条件,我相信你肯定可以写出代码来了,具体如下所示:
49+
50+
51+
```java
52+
class Solution {
53+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
54+
int m = obstacleGrid.length, n = obstacleGrid[0].length;
55+
int[][] dp = new int[m][n];
56+
// 其初始态第 1 列(行)的格子只有从其上(左)边格子走过去这一种走法,
57+
// 因此初始化 dp[i][0](dp[0][j])值为 1,且遇到障碍物时后面值都为 0;
58+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
59+
dp[i][0] = 1;
60+
}
61+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
62+
dp[0][j] = 1;
63+
}
64+
65+
for (int i = 1; i < m; i++) {
66+
for (int j = 1; j < n; j++) {
67+
if (obstacleGrid[i][j] == 0) {
68+
// 当 (i, j) 有障碍物时,dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
69+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
70+
}
71+
}
72+
}
73+
return dp[m - 1][n - 1];
74+
}
75+
}
76+
```
77+
78+
79+
## 结语
80+
81+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-java-leetcode][ajl]
82+
83+
84+
85+
[title]: https://leetcode-cn.com/problems/unique-paths-ii
86+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

src/com/blankj/medium/_0057/Solution.java src/com/blankj/hard/_0057/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.blankj.medium._057;
1+
package com.blankj.hard._0057;
22

33
import com.blankj.structure.Interval;
44

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.blankj.medium._0067;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2020/07/07
8+
* desc :
9+
* </pre>
10+
*/
11+
public class Solution {
12+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
13+
int m = obstacleGrid.length, n = obstacleGrid[0].length;
14+
int[][] dp = new int[m][n];
15+
// 其初始态第 1 列(行)的格子只有从其上(左)边格子走过去这一种走法,
16+
// 因此初始化 dp[i][0](dp[0][j])值为 1,且遇到障碍物时后面值都为 0;
17+
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
18+
dp[i][0] = 1;
19+
}
20+
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) {
21+
dp[0][j] = 1;
22+
}
23+
24+
for (int i = 1; i < m; i++) {
25+
for (int j = 1; j < n; j++) {
26+
if (obstacleGrid[i][j] == 0) {
27+
// 当 (i, j) 有障碍物时,dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
28+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
29+
}
30+
}
31+
}
32+
return dp[m - 1][n - 1];
33+
}
34+
35+
public static void main(String[] args) {
36+
Solution solution = new Solution();
37+
int[][] obstacleGrid = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
38+
System.out.println(solution.uniquePathsWithObstacles(obstacleGrid));
39+
}
40+
}

0 commit comments

Comments
 (0)