Traditional C code for you to reference~


  • 0
    Y
    int uniquePathsWithObstacles(int **obstacleGrid, int m, int n){
        if(obstacleGrid[m-1][n-1]||obstacleGrid[0][0])
            return 0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(obstacleGrid[i][j])
                    obstacleGrid[i][j]=INT_MIN;
            }
        }
        for(int i=0;i<n;i++){
            if(obstacleGrid[0][i])
                break;
            obstacleGrid[0][i]=1;
        }//first row
        for(int i=1;i<m;i++){
            if(obstacleGrid[i][0])
                break;
            obstacleGrid[i][0]=1;
        }//first col
    
        for(int row=1;row<m;row++){
            for(int col=1;col<n;col++){
                if(obstacleGrid[row][col])
                    continue;
                int up = obstacleGrid[row-1][col];
                if(up==INT_MIN) up = 0;
                int left = obstacleGrid[row][col-1];
                if(left==INT_MIN) left = 0;
                obstacleGrid[row][col]=up+left;
            }
        }
        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.