Unique Paths II

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]
Unique Paths II
 1 public class Solution {
 2     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
 3         int len = obstacleGrid.length;
 4         if(len==0) return 0;
 5         int len2 = obstacleGrid[0].length;
 6         if(obstacleGrid[0][0]==1) return 0;
 7         int[] max = new int [len2];
 8         max[0] =1;
 9         for(int i=0;i<len;i++){
10             for(int j=0;j<len2;j++){
11                 if(obstacleGrid[i][j]==1)
12                     max[j]=0;
13                 else if(j>0){
14                     max[j] = max[j]+max[j-1];
15                 }
16             }
17         }
18         return max[len2-1];
19     }
20 }
View Code

 

 

Unique Paths II

上一篇:《剑指Offer》面试题-从头到尾打印链表


下一篇:剑指Offer - 九度1524 - 复杂链表的复制