Simple Java solution, easy to understand


  • 0
    K
    public class Solution {
        public int uniquePathsWithObstacles(int[][] obstacleGrid) {
            int m=obstacleGrid.length;
            if(m==0)return 0;
            int n=obstacleGrid[0].length;
            if(n==0)return 1;
            if(n==1||m==1){
                for(int [] ii:obstacleGrid)for(int i:ii)if(i==1)return 0;
            }
            int[][] dp=new int[m][n];
            for(int i=0;i<m;i++){if(obstacleGrid[i][0]!=1)dp[i][0]=1;else break;}
            for(int i=0;i<n;i++){if(obstacleGrid[0][i]!=1)dp[0][i]=1;else break;}
            for(int j=1;j<m;j++){
                for(int k=1;k<n;k++){
                    if(obstacleGrid[j][k]==1)continue;
                    int tmp=0;
                    if(obstacleGrid[j-1][k]!=1)tmp+=dp[j-1][k];
                    if(obstacleGrid[j][k-1]!=1)tmp+=dp[j][k-1];
                    dp[j][k]=tmp;
                }
            }
            return dp[m-1][n-1];
        }
    }
    

Log in to reply
 

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