Basic java solution using DP


  • 0
    D
    public class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m=obstacleGrid.length,n=obstacleGrid[0].length;
            for(int i=0;i<m;i++){
                for(int j=0;j<n;j++){
                    if(obstacleGrid[i][j]==0){
                        if(i==0&&j==0){
                            obstacleGrid[i][j]=1;
                        }else if(i==0){
                            obstacleGrid[i][j]=obstacleGrid[i][j-1];
                        }else if(j==0){
                            obstacleGrid[i][j]=obstacleGrid[i-1][j];
                        }else{
                            obstacleGrid[i][j]=obstacleGrid[i][j-1]+obstacleGrid[i-1][j];
                        }
                    }else{
                        obstacleGrid[i][j]=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.