public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] paths = new int[m][n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(i==0 && j==0){//初始化起点
if(obstacleGrid[i][j]==1) return 0;
else paths[i][j] = 1;
}else if(obstacleGrid[i][j]==1){
paths[i][j] = 0;
}else if(i==0){
paths[i][j] = paths[i][j-1];
}else if(j==0){
paths[i][j] = paths[i-1][j];
}else{
paths[i][j] = paths[i-1][j] + paths[i][j-1];
}
}
}
return paths[m-1][n-1];
}
}
leetcode 63.Unique Paths II
最新推荐文章于 2023-08-09 08:45:25 发布