forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUniquePathsII.java
More file actions
47 lines (47 loc) · 1.21 KB
/
UniquePathsII.java
File metadata and controls
47 lines (47 loc) · 1.21 KB
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
package LeetCode;/**
* @Classname UniquePathsII
* @Description TODO
* @Date 19-2-28 上午11:11
* @Created by mao<tianmao818@qq.com>
*/
public class UniquePathsII {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m=obstacleGrid.length;
int n=obstacleGrid[0].length;
int[][] path=new int[m][n];
if(m==1 || n==1){
return 1;
}
for(int i=0;i<m;i++){
if(obstacleGrid[i][0]==1){
while (i<m){
path[i][0]=0;
i++;
}
}else {
path[i][0] = 1;
}
}
for(int j=0;j<n;j++){
path[0][j]=1;
if(obstacleGrid[0][j]==1){
while (j<n){
path[0][j]=0;
j++;
}
}else {
path[0][j]=1;
}
}
for(int x=1;x<m;x++){
for(int y=1;y<n;y++){
if(obstacleGrid[x][y]==1){
path[x][y]=0;
}else {
path[x][y] = path[x - 1][y] + path[x][y - 1];
}
}
}
return path[m-1][n-1];
}
}