- 左上角那个点:有obstacle,则为0;没有obstacle,则为1。
- 最上一排:有obstacle,则为0;没有obstacle,则为左边元素的值。
- 最左边一排:有obstacle,则为0;没有obstacle,则为上边元素的值。
- 其它点:有obstacle,则为0;没有obstacle,则为上边元素和左边元素值得和。
Implementation
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int right = obstacleGrid[0].length;
int down = obstacleGrid.length;
int[] record = new int[right];
for (int i = 0; i < down; i++) {
for (int j = 0; j < right; j++) {
if (i == 0 && j == 0) {
record[j] = (obstacleGrid[i][j] == 1)? 0: 1;
}
else if (i == 0) {
record[j] = (obstacleGrid[i][j] == 1)? 0: record[j - 1];
}
else if (j == 0) {
record[j] = (obstacleGrid[i][j] == 1)? 0: record[j];
}
else {
record[j] = (obstacleGrid[i][j] == 1)? 0: record[j] + record[j - 1];
}
}
}
return record[right - 1];
}
}