-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniquePathsII.java
More file actions
39 lines (36 loc) · 1.15 KB
/
UniquePathsII.java
File metadata and controls
39 lines (36 loc) · 1.15 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
package com.leetcode;
/**
* Created by jamylu on 2018/1/28.
* leetcode063.
* 矩阵中存在障碍
*/
public class UniquePathsII {
public static void main(String[] args) {
int obstacleGrid[][] = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
System.out.println(uniquePathsWithObstacles(obstacleGrid));
}
public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length; // rows
int n = obstacleGrid[0].length; // cols
int i, j;
int dp[][] = new int[m][n];
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
if (obstacleGrid[i][j] == 1) {
dp[i][j] = 0;
} else {
if (i == 0 && j == 0) {
dp[i][j] = 1;
} else if (i == 0) {
dp[i][j] = dp[i][j - 1];
} else if (j == 0) {
dp[i][j] = dp[i - 1][j];
} else {
dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
}
}
}
}
return dp[m - 1][n - 1];
}
}