class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int h = obstacleGrid.size();
if(h == 0){
return 0;
}
int w = obstacleGrid[0].size();
if(w == 0){
return 0;
}
vector<int>dp;
dp.resize(w, 0);
dp[0] = obstacleGrid[0][0] == 1 ? 0: 1;
if(dp[0] == 0){
return 0;
}
for(int i = 1; i < w; ++i){
dp[i] = obstacleGrid[0][i] == 1 ? 0: (dp[i - 1]);
}
for(int i = 1; i < h; ++i){
dp[0] = (dp[0] == 0 || obstacleGrid[i][0] == 1) ? 0 : 1;
for(int j = 1; j < w; ++j){
dp[j] = obstacleGrid[i][j] == 1 ? 0: (dp[j] + dp[j-1]);
}
}
return dp[w - 1];
}
};
错误1:
1. 矩阵的行和列傻傻分不清楚..下标写错..runtime error
2. 初始化dp[0]时忘记了前面的obstacle对后面的影响