Easy to understand java solution


  • 0
    L
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return 0;
            int m = obstacleGrid.length;
            int n = obstacleGrid[0].length;
            for (int i = 0; i < m; i++)
                for (int j = 0; j < n; j++) {
                    obstacleGrid[i][j] = 1 - obstacleGrid[i][j];
                    if (obstacleGrid[i][j] > 0) { //not obstacke
                        if (i != 0 || j!= 0)
                            obstacleGrid[i][j] = ((i > 0) ? obstacleGrid[i-1][j] : 0) + ((j > 0) ? obstacleGrid[i][j-1] : 0);
                }
            }
            
            return obstacleGrid[m-1][n-1];
        }
    

Log in to reply
 

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