Created
September 4, 2014 01:49
-
-
Save movefast/2bf80131abf19db89db1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public static int uniquePathsWithObstacles(int[][] obstacleGrid) { | |
if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return 0; | |
int[][] res = new int[obstacleGrid.length][obstacleGrid[0].length]; | |
for(int i = 0; i < obstacleGrid.length; i++) { | |
for(int j = 0; j < obstacleGrid[0].length;j++) { | |
if(obstacleGrid[i][j] == 1) res[i][j] = 0; | |
else if(i == 0 && j == 0) res[0][0] = 1; | |
else if (i == 0) res[i][j] = res[i][j-1]; | |
else if (j == 0) res[i][j] = res[i-1][j]; | |
else res[i][j] = res[i-1][j]+res[i][j-1]; | |
} | |
} | |
return res[obstacleGrid.length-1][obstacleGrid[0].length-1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment