[LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.
Note: m and n will be at most 100.
Example 1:
Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right
这道题是之前那道 Unique Paths 的延伸,在路径中加了一些障碍物,还是用动态规划 Dynamic Programming 来解,使用一个二维的 dp 数组,大小为 (m+1) x (n+1),这里的 dp[i][j] 表示到达 (i-1, j-1) 位置的不同路径的数量,那么i和j需要更新的范围就是 [1, m] 和 [1, n]。状态转移方程跟之前那道题是一样的,因为每个位置只能由其上面和左面的位置移动而来,所以也是由其上面和左边的 dp 值相加来更新当前的 dp 值,如下所示:
dp[i][j] = dp[i-1][j] + dp[i][j-1]
这里就能看出来初始化 d p数组的大小为 (m+1) x (n+1),是为了 handle 边缘情况,当i或j为0时,减1可能会出错。当某个位置是障碍物时,其 dp 值为0,直接跳过该位置即可。这里还需要初始化 dp 数组的某个值,使得其能正常累加。当起点不是障碍物时,其 dp 值应该为1,即dp[1][1] = 1,由于其是由 dp[0][1] + dp[1][0] 更新而来,所以二者中任意一个初始化为1即可。由于之后 LeetCode 更新了这道题的 test case,使得使用 int 型的 dp 数组会有溢出的错误,所以改为使用 long 型的数组来避免 overflow,代码如下:
解法一:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty() || obstacleGrid[].empty() || obstacleGrid[][] == ) return ;
int m = obstacleGrid.size(), n = obstacleGrid[].size();
vector<vector<long>> dp(m + , vector<long>(n + , ));
dp[][] = ;
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
if (obstacleGrid[i - ][j - ] != ) continue;
dp[i][j] = dp[i - ][j] + dp[i][j - ];
}
}
return dp[m][n];
}
};
或者我们也可以使用一维 dp 数组来解,省一些空间,参见代码如下:
解法二:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty() || obstacleGrid[].empty() || obstacleGrid[][] == ) return ;
int m = obstacleGrid.size(), n = obstacleGrid[].size();
vector<long> dp(n, );
dp[] = ;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (obstacleGrid[i][j] == ) dp[j] = ;
else if (j > ) dp[j] += dp[j - ];
}
}
return dp[n - ];
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/63
类似题目:
Unique Paths III
参考资料:
https://leetcode.com/problems/unique-paths-ii/
https://leetcode.com/problems/unique-paths-ii/discuss/23250/Short-JAVA-solution
https://leetcode.com/problems/unique-paths-ii/discuss/23248/My-C%2B%2B-Dp-solution-very-simple!
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 63. Unique Paths II 不同的路径之二的更多相关文章
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
- [leetcode] 63. Unique Paths II (medium)
原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
随机推荐
- 转 让FPGA替代GPU的6大顾虑,你确定不看看吗?
最近FPGA又频频被各AI领域的巨头看好,比如微软.百度.科大讯飞都对FPGA应用前景有所期待.那么如果让你选择FPGA作为AI计算系统的主力军,你会有什么样的顾虑? 这几天,已经退役的AlphaGo ...
- 花了近十年的时间,整理出史上最全面Java面试题
1.String 是最基本的数据类型吗? 不是.Java中的基本数据类型只有8个:byte.short.int.long.float.double.char.boolean:除了基本类型(primit ...
- 钉钉企业内部H5微应用开发
企业内部H5微应用开发 分为 服务端API和前端API的开发,主要涉及到进入应用免登流程和JSAPI鉴权. JSAPI鉴权开发步骤: 1.创建H5微应用 登入钉钉开放平台(https://open-d ...
- web前端-框架jquery
1.jquery库 就是js的库 ,可以通过jquery语法简化js操作 ,如文档遍历 ,文档操作 ,事件处理 ,动画js定时器等等 2.引用 下载:https://www.bootcdn.cn/jq ...
- Linux nodejs 安装以及配置环境
从官网中下载nodejs 打开官网 https://nodejs.org/en/download/ 复制拿到链接,下载nodejs wget https://nodejs.org/dist/v10.1 ...
- Linux出现You have new mail in /var/spool/mail/root提示,关闭邮件提示清理内容的解决方案
Linux出现You have new mail in /var/spool/mail/root提示,关闭邮件提示的解决方案 有的时候敲一下回车,就出来You have new mail in /va ...
- 转 Fortofy扫描漏洞解决方案
Log Forging漏洞: 数据从一个不可信赖的数据源进入应用程序. 在这种情况下,数据经由CreditCompanyController.java 的第 53行进入 getParameter(). ...
- QString判断空 isEmpty
isEmpty Returns true if the string has no characters; otherwise returns false. QString().isEmpty(); ...
- DV型、OV型、EV型证书的主要区别
DV型和OV型证书的区别 DV和OV型证书最大的差别是:DV型证书不包含企业名称信息:而OV型证书包含企业名称信息,以下是两者差别对比表: DV OV 包含企业名称信息 否 是 验证公司名称 ...
- [linux] shell脚本编程-统计日志文件中的设备号发通知邮件
1.日志文件列表 比如:/data1/logs/2019/08/15/ 10.1.1.1.log.gz 10.1.1.2.log.gz 2.统计日志中的某关键字shell脚本 zcat *.gz|gr ...