Java Solution easy understand


  • 0
    Y
    class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m = obstacleGrid.length;
            int n = obstacleGrid[0].length;
            if(m == 0 || n == 0){
                return 0;
            }
            int[][] map = new int[m][n];
            for(int i = 0 ; i < m ;i++){
                if(obstacleGrid[i][0] == 1){
                    map[i][0] = 0;
                }
                else if(i > 0 && (map[i-1][0] == 0) )
                    map[i][0] = 0;
                else
                    map[i][0] = 1;
            }
            for(int i = 0 ; i < n ; i++){
                if(obstacleGrid[0][i] == 1)
                    map[0][i] = 0;
                else if(i > 0 &&(map[0][i-1] == 0))
                    map[0][i] = 0;
                else
                    map[0][i] = 1;
            }
            for(int i = 1; i < m; i++){
                for( int j = 1; j < n ;j++){
                    if(obstacleGrid[i][j] == 1){
                        map[i][j] = 0;
                    }
                    else{
                        map[i][j] = map[i-1][j] + map[i][j-1];
                    }
                }
            }
            return map[m-1][n-1];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.