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 } }));
}
相关推荐
皮皮林55114 分钟前
使用 Java + WebSocket 实现简单实时双人协同 pk 答题
java·websocket
码小凡2 小时前
优雅!用了这两款插件,我成了整个公司代码写得最规范的码农
java·后端
掉鱼的猫3 小时前
Solon AI 五步构建 RAG 服务:2025 最新 AI + 向量数据库实战
java·redis·后端
java金融4 小时前
FactoryBean 和BeanFactory的傻傻的总是分不清?
java·后端
独立开阀者_FwtCoder4 小时前
Nginx 通过匹配 Cookie 将请求定向到特定服务器
java·vue.js·后端
名曰大神4 小时前
AEM6.5集成Redis详细步骤(附代码)
java·redis·demo·aem
带刺的坐椅4 小时前
Solon AI 五步构建 RAG 服务:2025 最新 AI + 向量数据库实战
java·redis·ai·solon·rag
东阳马生架构5 小时前
商品中心—7.自研缓存框架的技术文档
java
晴空月明7 小时前
线程安全与锁机制深度解析
java
天天摸鱼的java工程师8 小时前
你如何处理一个高并发接口的线程安全问题?说说你做过的优化措施
java·后端