Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- initialize的时候,initial第一行的时候要看多少列,第一列的时候要看多少行。
- 后面function正常。
- public class Solution {
- /**
- * @param obstacleGrid: A list of lists of integers
- * @return: An integer
- */
- public int uniquePathsWithObstacles(int[][] obstacleGrid) {
- // write your code here
- if (obstacleGrid == null || obstacleGrid.length == 0 ||
- obstacleGrid[0].length == 0 || obstacleGrid[0][0] == 1) {
- return 0;
- }
- int[][] state = new int[obstacleGrid.length][obstacleGrid[0].length];
- //initialize
- for (int i = 0; i < obstacleGrid[0].length; i++) {
- if (i == 0) {
- state[0][i] = 1;
- } else if (obstacleGrid[0][i] == 1 || state[0][i - 1] == 0) {
- state[0][i] = 0;
- } else {
- state[0][i] = 1;
- }
- }
- for (int i = 1; i < obstacleGrid.length; i++) {
- if (obstacleGrid[i][0] == 1 || state[i - 1][0] == 0) {
- state[i][0] = 0;
- } else {
- state[i][0] = 1;
- }
- }
- for (int i = 1; i < obstacleGrid.length; i++) {
- for (int j = 1; j < obstacleGrid[0].length; j++) {
- if (obstacleGrid[i][j] == 1) {
- state[i][j] = 0;
- } else {
- state[i][j] = state[i - 1][j] + state[i][j - 1];
- }
- }
- }
- return state[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement