动态规划
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
// 遇到障碍则从(0,0)到达
for(int i = 0; i < m && obstacleGrid[i][0] == 0; i++){
dp[i][0] = 1;
}
for(int j = 0; j < n && obstacleGrid[0][j] == 0; j++){
dp[0][j] = 1;
}
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
// 如果没有障碍则dp公式正确执行
if(obstacleGrid[i][j] == 0){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
return dp[m - 1][n - 1];
}
}
本题目是上一篇博客不同路径这道题目的升级版,整体动态规划思路相同,本题多了障碍这一条件约束。
标签:obstacleGrid,题目,int,随想录,++,63,&&,Leetcode,dp From: https://blog.csdn.net/qq_46574748/article/details/140395751