63. Unique Paths II

 1 class Solution {
 2     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
 3         if(obstacleGrid == null) return 0;
 4         int row = obstacleGrid.length;
 5         int col = obstacleGrid[0].length;
 6         int[][] res = new int[row][col];
 7         if(obstacleGrid[0][0] != 1) {
 8             res[0][0] = 1;
 9         }
10 
11         for(int i = 0; i < row; i++) {
12             for(int j = 0; j < col; j++) {
13                 if(i == 0 && j == 0) continue;
14                 if(obstacleGrid[i][j] != 1) {
15                     if(i-1 < 0) {
16                         res[i][j] = res[i][j-1];
17                     }else if(j-1 < 0) {
18                         res[i][j] = res[i-1][j];
19                     }else {
20                         res[i][j] = res[i][j-1] + res[i-1][j];
21                     }
22                 }else {
23                     continue;
24                 }
25             }
26         }
27         return res[row-1][col-1];
28         
29     }
30 }

 

posted @ 2018-09-14 09:12  jasoncool1  阅读(105)  评论(0编辑  收藏  举报