LeetCode第63题 - 不同路径 II

题目

解答

java 复制代码
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];
    }
}

要点

本题目充分说明,使用动态规划解题时,初始值很重要。

另外,假如起点和终点均为障碍物的话,可以直接返回,不需要执行后续的求解操作。

准备的用例,如下

java 复制代码
@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 } }));
}
相关推荐
敖云岚8 分钟前
【Redis】Another Redis Desktop Manager 安装指南
java·redis·github
耀耀_很无聊6 小时前
02_使用 AES 算法实现文件加密上传至阿里云、解密下载
java·spring boot·算法·阿里云·云计算·aes·oss
GalaxySpaceX7 小时前
Hibernate-Core (CVE-2020-25638)
java·spring·hibernate
小钻风巡山8 小时前
springboot 视频分段加载在线播放
java·spring boot·后端
不会写代码的女程序猿8 小时前
废品回收小程序:全链路数字化解决方案,赋能绿色未来
java·小程序·微信小程序定制开发
江沉晚呤时9 小时前
Redis缓存穿透、缓存击穿与缓存雪崩:如何在.NET Core中解决
java·开发语言·后端·算法·spring·排序算法
hunandede9 小时前
av_dict_get,av_dict_set,av_dict_set_int
java·前端·javascript
predisw10 小时前
垃圾收集GC的基本理解
java·jvm·算法
帅得不敢出门10 小时前
Android Framework学习二:Activity创建及View绘制流程
android·java·学习·framework·安卓·activity·window
264玫瑰资源库10 小时前
红鸟3D互动系统源码一键部署教程(含多个打包版本与功能解构)
java·数据库·游戏