My accepted java in-place solution


  • 0
    D
    public class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            for(int row = 0; row < obstacleGrid.length;row++) {
                for(int col = 0; col < obstacleGrid[0].length;col++) {
                    if(row == 0 && col ==0) {
                        if (obstacleGrid[0][0] == 1) {
                            return 0;
                        }else {
                            obstacleGrid[0][0] = 1;
                        }
                    }else if(row - 1 < 0) {
                        if(obstacleGrid[row][col] == 1) {
                            obstacleGrid[row][col] = 0;
                        }else {
                            obstacleGrid[row][col] = obstacleGrid[row][col - 1];
                        }
                    }else if(col - 1 < 0) {
                         if(obstacleGrid[row][col] == 1) {
                            obstacleGrid[row][col] = 0;
                        }else {
                            obstacleGrid[row][col] = obstacleGrid[row - 1][col];
                        }
                    
                    }else {
                         if(obstacleGrid[row][col] == 1) {
                            obstacleGrid[row][col] = 0;
                        }else {
                            obstacleGrid[row][col] = obstacleGrid[row-1][col] + obstacleGrid[row][col - 1]; 
                        }
                    }
                }
            }
            return obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1];
        }
    }

Log in to reply
 

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