LeetCode第63题 - 不同路径 II

题目

解答

class Solution {
   

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
   
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        if (obstacleGrid[0][0] == 1) {
   
            return 0;
        }

        if (obstacleGrid[m - 1][n - 1] == 1) {
   
            return 0;
        }

        int[][] dp = new int[m][n];

        dp[0][0] = 1;
        for (int i = 1, imax = m; i < imax; ++i) {
   
            if (obstacleGrid[i][0] == 1) {
   
                dp[i][0] = 0;
            } else {
   
                dp[i][0] = dp[i - 1][0];
            }
        }

        for (int j = 1, jmax = n; j < jmax; ++j) {
   
            if (obstacleGrid[0][j] == 1) {
   
                dp[0][j] = 0;
            } else {
   
                dp[0][j] = dp[0][j - 1];
            }
        }

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

                    if (obstacleGrid[i][j - 1] == 0) {
   
                        dp[i][j] += dp[i][j - 1];
                    }
                }

            }
        }

        return dp[m - 1][n - 1];
    }
}

要点
本题目充分说明,使用动态规划解题时,初始值很重要。
另外,假如起点和终点均为障碍物的话,可以直接返回,不需要执行后续的求解操作。

准备的用例,如下

@Before
public void before() {
   
    t = new Solution();
}

@Test
public void test001() {
   
    assertEquals(2, t.uniquePathsWithObstacles(new int[][] {
    {
    0, 0, 0 }, {
    0, 1, 0 }, {
    0, 0, 0 } }));
}

@Test
public void test002() {
   
    assertEquals(1, t.uniquePathsWithObstacles(new int[][] {
    {
    0, 1 }, {
    0, 0 } }));
}

@Test
public void test003() {
   
    assertEquals(1, t.uniquePathsWithObstacles(new int[][] {
    {
    0, 0 } }));
}

@Test
public void test004() {
   
    assertEquals(0, t.uniquePathsWithObstacles(new int[][] {
    {
    0, 0 }, {
    1, 1 }, {
    0, 0 } }));
}

@Test
public void test005() {
   
    assertEquals(0, t.uniquePathsWithObstacles(new int[][] {
    {
    0, 0 }, {
    0, 1 } }));
}

@Test
public void test006() {
   
    assertEquals(0, t.uniquePathsWithObstacles(new int[][] {
    {
    1, 0 }, {
    0, 0 } }));
}

@Test
public void test007() {
   
    assertEquals(0, t.uniquePathsWithObstacles(
            new int[][] {
    {
    0, 1, 0, 0, 0 }, {
    1, 0, 0, 0, 0 }, {
    0, 0, 0, 0, 0 }, {
    0, 0, 0, 0, 0 } }));
}

相关推荐

  1. LeetCode63 - 不同路径 II

    2023-12-30 02:42:04       43 阅读
  2. leetcode 63.不同路径II

    2023-12-30 02:42:04       14 阅读
  3. Leetcode63- 不同路径II

    2023-12-30 02:42:04       11 阅读
  4. 动态规划 Leetcode 63 不同路径II

    2023-12-30 02:42:04       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 02:42:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 02:42:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 02:42:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 02:42:04       18 阅读

热门阅读

  1. LeetCode1822. Sign of the Product of an Array

    2023-12-30 02:42:04       30 阅读
  2. P1308 [NOIP2011 普及组] 统计单词数----有意思

    2023-12-30 02:42:04       29 阅读
  3. YCSB 测试表预分区

    2023-12-30 02:42:04       36 阅读
  4. Netty学习

    2023-12-30 02:42:04       41 阅读
  5. 聊一聊Spring Bean 的生命周期

    2023-12-30 02:42:04       35 阅读
  6. 【PHP】API传参时,判断数字最多两位小数

    2023-12-30 02:42:04       36 阅读
  7. 服务简介及问题答疑

    2023-12-30 02:42:04       31 阅读