File tree Expand file tree Collapse file tree 3 files changed +103
-0
lines changed
solution/063.Unique Paths II Expand file tree Collapse file tree 3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ Complete solutions to Leetcode problems, updated daily.
31
31
| 019 | [ Remove Nth Node From End of List] ( https://github.com/yanglbme/leetcode/tree/master/solution/019.Remove%20Nth%20Node%20From%20End%20of%20List ) | ` Linked List ` , ` Two Pointers ` |
32
32
| 024 | [ Swap Nodes in Pairs] ( https://github.com/yanglbme/leetcode/tree/master/solution/024.Swap%20Nodes%20in%20Pairs ) | ` Linked List ` |
33
33
| 062 | [ Unique Paths] ( https://github.com/yanglbme/leetcode/tree/master/solution/062.Unique%20Paths ) | ` Array ` , ` Dynamic Programming ` |
34
+ | 063 | [ Unique Paths II] ( https://github.com/yanglbme/leetcode/tree/master/solution/063.Unique%20Paths%20II ) | ` Array ` , ` Dynamic Programming ` |
34
35
| 082 | [ Remove Duplicates from Sorted List II] ( https://github.com/yanglbme/leetcode/tree/master/solution/082.Remove%20Duplicates%20from%20Sorted%20List%20II ) | ` Linked List ` |
35
36
36
37
Original file line number Diff line number Diff line change
1
+ ## 不同路径 II
2
+ ### 题目描述
3
+
4
+ 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
5
+
6
+ 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
7
+
8
+ 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
9
+
10
+ ![ robot_maze] ( http://p9ucdlghd.bkt.clouddn.com/robot_maze.png )
11
+
12
+ 网格中的障碍物和空位置分别用 1 和 0 来表示。
13
+
14
+ 说明:m 和 n 的值均不超过 100。
15
+
16
+ 示例 1:
17
+ ```
18
+ 输入:
19
+ [
20
+ [0,0,0],
21
+ [0,1,0],
22
+ [0,0,0]
23
+ ]
24
+ 输出: 2
25
+ 解释:
26
+ 3x3 网格的正中间有一个障碍物。
27
+ 从左上角到右下角一共有 2 条不同的路径:
28
+ 1. 向右 -> 向右 -> 向下 -> 向下
29
+ 2. 向下 -> 向下 -> 向右 -> 向右
30
+ ```
31
+
32
+ ### 解法
33
+ 与上题相比,这一题仅仅多了一个条件,就是网格中存在障碍物。对于最左侧和最上方,如果存在障碍物,那么往右或者往下的网格中路径均为 0。而其它格子,如果该格子有障碍物,那么路径为 0,否则是它“左方格子的路径数+上方格子的路径数之和”(递推式)。同样开辟一个二维数组存放中间结果。
34
+
35
+ ``` java
36
+ class Solution {
37
+ public int uniquePathsWithObstacles (int [][] obstacleGrid ) {
38
+ int n = obstacleGrid. length;
39
+ int m = obstacleGrid[0 ]. length;
40
+ int [][] res = new int [n][m];
41
+ int i = 0 ;
42
+ while (i < n && obstacleGrid[i][0 ] == 0 ) {
43
+ // 无障碍物
44
+ res[i++ ][0 ] = 1 ;
45
+ }
46
+ while (i < n) {
47
+ res[i++ ][0 ] = 0 ;
48
+ }
49
+
50
+ i = 0 ;
51
+ while (i < m && obstacleGrid[0 ][i] == 0 ) {
52
+ // 无障碍物
53
+ res[0 ][i++ ] = 1 ;
54
+ }
55
+ while (i < m) {
56
+ res[0 ][i++ ] = 0 ;
57
+ }
58
+
59
+ for (int k = 1 ; k < n; ++ k) {
60
+ for (int j = 1 ; j < m; ++ j) {
61
+ res[k][j] = obstacleGrid[k][j] == 1 ? 0 : (res[k - 1 ][j] + res[k][j - 1 ]);
62
+ }
63
+ }
64
+
65
+ return res[n - 1 ][m - 1 ];
66
+
67
+ }
68
+ }
69
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int uniquePathsWithObstacles (int [][] obstacleGrid ) {
3
+ int n = obstacleGrid .length ;
4
+ int m = obstacleGrid [0 ].length ;
5
+ int [][] res = new int [n ][m ];
6
+ int i = 0 ;
7
+ while (i < n && obstacleGrid [i ][0 ] == 0 ) {
8
+ // 无障碍物
9
+ res [i ++][0 ] = 1 ;
10
+ }
11
+ while (i < n ) {
12
+ res [i ++][0 ] = 0 ;
13
+ }
14
+
15
+ i = 0 ;
16
+ while (i < m && obstacleGrid [0 ][i ] == 0 ) {
17
+ // 无障碍物
18
+ res [0 ][i ++] = 1 ;
19
+ }
20
+ while (i < m ) {
21
+ res [0 ][i ++] = 0 ;
22
+ }
23
+
24
+ for (int k = 1 ; k < n ; ++k ) {
25
+ for (int j = 1 ; j < m ; ++j ) {
26
+ res [k ][j ] = obstacleGrid [k ][j ] == 1 ? 0 : (res [k - 1 ][j ] + res [k ][j - 1 ]);
27
+ }
28
+ }
29
+
30
+ return res [n - 1 ][m - 1 ];
31
+
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments