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).
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
分析:
和第62题思路类似,LeetCode 62. Unique Paths不同路径 (C++/Java)
现在网格中有了障碍物,网格中的障碍物和空位置分别用1和 0来表示。
无论是递推还是递归求解,只要加一个判断当前各自是否有障碍即可。在判断当前是否有解时,可以在最开始将二维数组全部赋值-1,如果求到子问题时不为-1,则可以直接返回已有的解,可以节省时间。
注:1.起始位置可能有障碍物。
2.还遇到一个问题: runtime error: signed integer overflow: 1053165744 + 1579748616 cannot be represented in type 'int' (solution.cpp),改成long即可。
程序:
C++
//Solution 1
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
vector<vector<long>> res(m+, vector<long>(n+, ));
for(int i = ; i < m+; ++i)
for(int j = ; j < n+; ++j){
if(obstacleGrid[i-][j-] == ){
res[i][j] = ;
}
else if(i == && j == ){
res[i][j] = ;
}
else{
res[i][j] = res[i-][j] + res[i][j-];
}
}
return res[m][n];
}
};
//Solution 2
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
res = vector<vector<int>>(m+, vector<int>(n+, -)); return solvePath(m, n, obstacleGrid);
}
private:
vector<vector<int>> res;
int solvePath(int m, int n, vector<vector<int>> &vec){
if(m <= || n <= ) return ;
if(m == && n == ) return -vec[][];
if(res[m][n] != -) return res[m][n];
if(vec[m-][n-] == ){
res[m][n] = ;
}
else{
res[m][n] = solvePath(m-, n, vec) + solvePath(m, n-, vec);
}
return res[m][n];
}
};
Java
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
res = new int[m+1][n+1];
for(int[] r : res)
Arrays.fill(r,-1);
return solvePath(m, n, obstacleGrid);
}
private int[][] res;
private int solvePath(int m, int n, int[][] o){
if(m <= 0 || n <= 0) return 0;
if(m == 1 && n == 1) return 1-o[0][0];
if(res[m][n] != -1) return res[m][n];
if(o[m-1][n-1] == 1){
res[m][n] = 0;
}
else{
res[m][n] = solvePath(m-1, n, o) + solvePath(m, n-1, o);
}
return res[m][n];
}
}
LeetCode 63. Unique Paths II不同路径 II (C++/Java)的更多相关文章
- [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 ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] 62. Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 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 唯一路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
随机推荐
- 201871010123-吴丽丽《面向对象程序设计(Java)》第十三周学习总结
201871010123-吴丽丽<面向对象程序设计(Java)>第十三周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- xpath:
from selenium import webdriverb = webdriver.Firefox()#路径读取方式一:# b.get(r"C:\我的代码\selenium自动化测试\t ...
- USACO Max Flow
洛谷 P3128 [USACO15DEC]最大流Max Flow 洛谷传送门 JDOJ 3027: USACO 2015 Dec Platinum 1.Max Flow JDOJ传送门 Descrip ...
- [译]ABP v1.0终于发布了!
ABP v1.0终于发布了! 今天是个大日子!经过约3年的不断开发,第一个稳定的ABP版本,1.0,已经发布了.感谢为该项目做出贡献或试用过的每个人. 立即开始使用新的ABP框架:abp.io/get ...
- 《大数据技术应用与原理》第二版-第二章大数据处理架构Hadoop
2.1概述 Hadoop是Apache旗下的开源分布式计算平台,是基于Java开发的,具有很好的跨平台特性,其中核心文件是MapReduce和HDFS,而HDFS是根据谷歌文件系统GFS开源实现,是面 ...
- Matplotlib绘图及动画总结
目录 Matplotlib绘图总结 绘图原理 block模式(python默认) interactive模式(ipython模式默认) 深入子图 子图表示 子图绘图 绘制动画 参考链接 Matplot ...
- Java连载45-继承举例、方法覆盖
一.Java语言中假设一个类没有显式的继承任何类,那么该类默认继承Java SE库中提供的java.lang.Object类 1.快捷键:Ctrl + shift + T:可以在Myeclipse中查 ...
- 一篇文章弄懂flex布局
壹 ❀ 引 谈到flex布局,我不知道有多少人跟我一样,在本能的想到justify-content:center与align-items:center两条属性之后,除此之外的其它属性居然显得格外陌生 ...
- vue 渐变 进度条 progress
废话 不多少说 ,直接上代码 新建文件 gradual-progress.vue <!-- * @Author: gfc * @Date: 2019-11-07 14:00:11 * @Last ...
- mysql压缩备份导入导出
mysqldump工具自带选项没有对导出备份文件压缩功能,可结合gzip只使用一条命令压缩导出文件,方法如下: mysqldump压缩导出:# mysqldump -h192.168.0.3 -P33 ...