Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
58 lines (55 loc) · 1.88 KB

File metadata and controls

58 lines (55 loc) · 1.88 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.yangchd.leetcode.medium;
/**
* @author yangchd 2018/10/21
*
* 63.Unique Paths II
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
* The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
* Now consider if some obstacles are added to the grids. How many unique paths would there be?
*
* Note: m and n will be at most 100.
*
* Example 1:
* Input:
* [
* [0,0,0],
* [0,1,0],
* [0,0,0]
* ]
* Output: 2
* Explanation:
* There is one obstacle in the middle of the 3x3 grid above.
* There are two ways to reach the bottom-right corner:
* 1. Right -> Right -> Down -> Down
* 2. Down -> Down -> Right -> Right
*
*/
public class UniquePathsII {
/**
* 后一步的可能是左上两部分相加
*/
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (obstacleGrid[r][c] == 1) {
obstacleGrid[r][c] = 0;
} else {
if (r == 0 && c == 0) {
obstacleGrid[r][c] = 1;
} else if (r == 0) {
obstacleGrid[r][c] = obstacleGrid[r][c - 1];
} else if (c == 0) {
obstacleGrid[r][c] = obstacleGrid[r - 1][c];
} else {
obstacleGrid[r][c] = obstacleGrid[r][c - 1] + obstacleGrid[r - 1][c];
}
}
}
}
return obstacleGrid[m - 1][n - 1];
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.