63.不同路径II

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int[][] result = new int[obstacleGrid.length][obstacleGrid[0].length];
        if(obstacleGrid[0][0] == 1 || obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1] == 1){
            return 0;
        }
        for(int i = 0; i < obstacleGrid.length; i++){
            if(obstacleGrid[i][0] == 0){
                result[i][0] = 1;
            } else {
                break;
            }
        }
        for(int i = 0; i < obstacleGrid[0].length; i++){
            if(obstacleGrid[0][i] == 0){
                result[0][i] = 1;
            } else {
                break;
            }
        }
        for(int i = 1; i < obstacleGrid.length; i++){
            for(int j = 1; j < obstacleGrid[0].length; j++){
                if(obstacleGrid[i][j] == 0){
                    result[i][j] = result[i-1][j] + result[i][j-1];
                }
            }
        }
        return result[result.length-1][result[0].length-1];
    }
}

你可能感兴趣的:(63.不同路径II)