UniquePathII

  • 2022-12-14
  • 浏览 (484)

UniquePathII.java 源码

package algorithm.dp;

/**
 * @author roseduan
 * 不同路径II
 */
public class UniquePathII {

    public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
            dp[i][0] = 1;
        }
        for (int i = 0; i < n && obstacleGrid[0][i] == 0; i++) {
            dp[0][i] = 1;
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 0) {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m - 1][n - 1];
    }

    public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 || j == 0) {
                    if (i > 0 && obstacleGrid[i][j] == 0 && dp[i - 1][j] == 1) {
                        dp[i][j] = 1;
                    }
                    if (j > 0 && obstacleGrid[i][j] == 0 && dp[i][j - 1] == 1) {
                        dp[i][j] = 1;
                    }
                    if (obstacleGrid[i][j] == 0 && i == 0 && j == 0) {
                        dp[i][j] = 1;
                    }
                } else if (obstacleGrid[i][j] == 0) {
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}

你可能感兴趣的文章

CoinChange

EditDistance

HouseRobber

0  赞