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 } }));
}
相关推荐
litGrey13 分钟前
Maven国内镜像(四种)
java·数据库·maven
丶白泽38 分钟前
重修设计模式-结构型-桥接模式
java·设计模式·桥接模式
o独酌o44 分钟前
递归的‘浅’理解
java·开发语言
无问8171 小时前
数据结构-排序(冒泡,选择,插入,希尔,快排,归并,堆排)
java·数据结构·排序算法
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS在线文档管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Flying_Fish_roe2 小时前
Spring Boot-版本兼容性问题
java·spring boot·后端
程序猿进阶2 小时前
如何在 Visual Studio Code 中反编译具有正确行号的 Java 类?
java·ide·vscode·算法·面试·职场和发展·架构
程序猿练习生2 小时前
C++速通LeetCode中等第5题-无重复字符的最长字串
开发语言·c++·leetcode
slandarer2 小时前
MATLAB | R2024b更新了哪些好玩的东西?
java·数据结构·matlab
Dola_Pan2 小时前
Linux文件IO(一)-open使用详解
java·linux·dubbo