63. Unique Paths II
作者:
刘小小gogo | 来源:发表于
2018-09-04 23:32 被阅读0次

image.png

image.png
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int>> dp(m);
for(int i = 0; i < m; i++){
dp[i].resize(n);
}
if(obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1) return 0;
dp[0][0] = 1;
for(int i = 0; i < m ; i++){
for(int j = 0; j < n;j++){
if(i == 0 & j == 0) continue;//一定要注意,不能把dp[0][0]设为0,否则全部都是0;
int up = (i - 1 >= 0 && obstacleGrid[i - 1][j] == 0) ? dp[i - 1][j] : 0;
int left = (j - 1 >= 0 && obstacleGrid[i][j - 1] == 0) ? dp[i][j - 1] : 0;
dp[i][j] = up + left;
}
}
return dp[m - 1][n - 1];
}
};
本文标题:63. Unique Paths II
本文链接:https://www.haomeiwen.com/subject/pzhowftx.html
网友评论