不同路径二
https://leetcode.cn/problems/unique-paths-ii/submissions/
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
//dp数组 dp[i][j] 表示从0 0 到 i j 路径个数
//递推公式仍为dp[i][j] = dp[i-1][j]+ dp[i][j-1]
//初始化条件:dp[i][0] = dp[0][j] = 1
//当第一行或第一列存在障碍时
//障碍右边或下边 应初始化为0
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
//行初始化 i < m && obstacleGrid[i][0] == 0条件不满足 直接跳出for循环
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;
}
//打印dp数组
// for(int i =0 ;i < m; i++){
// for(int j = 0 ;j < n ;j++){
// System.out.print(dp[i][j] + " ");
//
// }
// System.out.print("\n");
// }
for(int i = 1; i < m ; i++){
for(int j = 1; j < n ; j++){
if(obstacleGrid[i][j] == 1){
continue;
}
dp[i][j] = dp[i-1][j]+ dp[i][j-1];
}
}
System.out.println(dp[m-1][n-1]);
return dp[m-1][n-1];
// return 0;
}
}