UniquePathII
UniquePathII.java 源码
package algorithm.dp;
/**
* @author roseduan
* 不同路径II
*/
public class UniquePathII {
public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
dp[i][0] = 1;
}
for (int i = 0; i < n && obstacleGrid[0][i] == 0; i++) {
dp[0][i] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (obstacleGrid[i][j] == 0) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0) {
if (i > 0 && obstacleGrid[i][j] == 0 && dp[i - 1][j] == 1) {
dp[i][j] = 1;
}
if (j > 0 && obstacleGrid[i][j] == 0 && dp[i][j - 1] == 1) {
dp[i][j] = 1;
}
if (obstacleGrid[i][j] == 0 && i == 0 && j == 0) {
dp[i][j] = 1;
}
} else if (obstacleGrid[i][j] == 0) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦