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 } }));
}
相关推荐
virus59453 小时前
悟空CRM mybatis-3.5.3-mapper.dtd错误解决方案
java·开发语言·mybatis
一匹电信狗3 小时前
【LeetCode_547_990】并查集的应用——省份数量 + 等式方程的可满足性
c++·算法·leetcode·职场和发展·stl
鱼跃鹰飞4 小时前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
没差c4 小时前
springboot集成flyway
java·spring boot·后端
时艰.4 小时前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
编程彩机5 小时前
互联网大厂Java面试:从Java SE到大数据场景的技术深度解析
java·大数据·spring boot·面试·spark·java se·互联网大厂
笨蛋不要掉眼泪5 小时前
Spring Boot集成LangChain4j:与大模型对话的极速入门
java·人工智能·后端·spring·langchain
Yvonne爱编码5 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python
We་ct6 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
像少年啦飞驰点、6 小时前
零基础入门 Spring Boot:从“Hello World”到可上线微服务的完整学习指南
java·spring boot·微服务·编程入门·后端开发