代码随想录——不同路径Ⅱ(Leetcode 63)

题目链接

动态规划

java 复制代码
class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        // 遇到障碍则从(0,0)到达
        for(int i = 0; i < m && obstacleGrid[i][0] == 0; i++){
            dp[i][0] = 1;
        }
        for(int j = 0; j < n && obstacleGrid[0][j] == 0; j++){
            dp[0][j] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
            	// 如果没有障碍则dp公式正确执行
                if(obstacleGrid[i][j] == 0){
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}

本题目是上一篇博客不同路径这道题目的升级版,整体动态规划思路相同,本题多了障碍这一条件约束。

相关推荐
litGrey20 分钟前
Maven国内镜像(四种)
java·数据库·maven
丶白泽1 小时前
重修设计模式-结构型-桥接模式
java·设计模式·桥接模式
o独酌o1 小时前
递归的‘浅’理解
java·开发语言
无问8171 小时前
数据结构-排序(冒泡,选择,插入,希尔,快排,归并,堆排)
java·数据结构·排序算法
Lenyiin1 小时前
《 C++ 修炼全景指南:十 》自平衡的艺术:深入了解 AVL 树的核心原理与实现
数据结构·c++·stl
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS在线文档管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
哲伦贼稳妥2 小时前
程序人生-我的外服经历(4)
经验分享·程序人生·职场和发展
Flying_Fish_roe2 小时前
Spring Boot-版本兼容性问题
java·spring boot·后端
程序猿进阶2 小时前
如何在 Visual Studio Code 中反编译具有正确行号的 Java 类?
java·ide·vscode·算法·面试·职场和发展·架构
Eloudy2 小时前
一个编写最快,运行很慢的 cuda gemm kernel, 占位 kernel
算法