方法一 动态规划
由于 dp[i][j] = dp[i-1][j]+dp[i][j-1] 等于 dp[j]+=dp[j-1],其中 dp[j] 就相当于 dp[i-1][j],dp[i][j-1] 就相当于 dp[j-1],就从二维压缩到一维了。
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int n = obstacleGrid.length, m = obstacleGrid[0].length;
int[] res = new int[m];
res[0] = obstacleGrid[0][0] == 0 ? 1 : 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(obstacleGrid[i][j] == 1){
res[j] = 0;
continue;
}
if(j - 1 >= 0 && obstacleGrid[i][j - 1] == 0){
res[j] += res[j - 1];
}
}
}
return res[m - 1];
}
}