Created
November 5, 2012 16:44
-
-
Save zac-xin/4018234 to your computer and use it in GitHub Desktop.
UniquePath2FromLeetcode
This file contains hidden or 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 int uniquePathsWithObstacles(int[][] obstacleGrid) { | |
| int m = obstacleGrid.length; | |
| int n = obstacleGrid[0].length; | |
| int matrix[][] = new int[m][n]; | |
| for(int i = n - 1; i >= 0; i--){ | |
| if(obstacleGrid[m - 1][i] == 1) | |
| break; | |
| matrix[m - 1][i] = 1; | |
| } | |
| for(int i = m - 1; i >=0; i--){ | |
| if(obstacleGrid[i][n-1] == 1) | |
| break; | |
| matrix[i][n-1] = 1; | |
| } | |
| for(int i = m - 2; i >= 0; i--){ | |
| for(int j = n - 2; j >=0; j--){ | |
| if(obstacleGrid[i][j] == 1) | |
| matrix[i][j] = 0; | |
| else | |
| matrix[i][j] = matrix[i+1][j] + matrix[i][j+1]; | |
| } | |
| } | |
| return matrix[0][0]; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment