基本思路,在用Step_Num[ i ][ j ]表示做到 i 、j这个点有多少种走法,那么Step_Num[ i ][ j ]=Step_Num[ i-1 ][ j ]+Step_Num[ i ][ j-1 ],当然咱们要保证 i-1和j-1要大于0才行,不然就数组越界了。下面是AC解:
AC解:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int hang = obstacleGrid.size();
int lie = obstacleGrid[0].size();
if (obstacleGrid[0][0] == 1|| obstacleGrid[hang - 1][lie - 1] == 1)
return 0;
vector<vector<int>> Step_Num(obstacleGrid.size(), vector<int>(obstacleGrid[0].size(), 0));
Step_Num[0][0] = 1;
for (int i = 0; i < hang; i++)
{
for (int j = 0; j < lie; j++)
{
if (obstacleGrid[i][j] != 1 && !(i == 0 && j == 0))
Step_Num[i][j] = (j > 0 ? Step_Num[i][j - 1] : 0) + (i > 0 ? Step_Num[i - 1][j] : 0);
}
}
return Step_Num[hang - 1][lie - 1];
}
};