Unique Paths IITotal accepted:31019 Total submissions:110866my submissions QuestionSolution
Follow up for "Unique Paths":
Now consider if some obstacles is added to the grids. How many unique paths would there be?
An obstacle and empty space are marked as and respectively in the 1
0
grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0]
The total number of unique paths is 2
.
Note: m and N would be is at most 100.
Soulution 1:
Compared with leetcode:unique Paths problem-solving report, it is just to judge whether the current block, if it is block, directly set D[I][J] is 0 is good is not up. Also within 3 minutes bug free seconds answer, oh yes!
1 Public classSolution {2 Public intUniquepathswithobstacles (int[] obstaclegrid) {3 //11504 if(Obstaclegrid = =NULL|| Obstaclegrid.length = =0|| obstaclegrid[0].length = =0) {5 return 0;6 }7 8 introws =obstaclegrid.length;9 intcols = obstaclegrid[0].length;Ten One int[] D =New int[Rows][cols]; A - for(inti =0; i < rows; i++) { - for(intj =0; J < cols; J + +) { theD[I][J] =0; - if(Obstaclegrid[i][j] = =1) { -D[I][J] =0; -}Else { + if(i = =0&& J = =0) { -D[I][J] =1; + } A at if(I! =0) { -D[I][J] + = d[i-1][j]; - } - - if(J! =0) { -D[I][J] + = d[i][j-1]; in } - } to } + } - the returnD[rows-1][cols-1]; * } $}
View Code
Leetcode:unique Paths II Problem Solving report