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 ...
随机推荐
- 解决plsql显示问号(???)问题
如何查看Oracle数据库的字符编码 1.查询Oracle Server端的字符集:有很多种方法可以查出oracle server端的字符集,比较直观的查询方法是以下这种:SQL>select ...
- <Graph> Topological + Undirected Graph 310 Union Find 261 + 323 + (hard)305
310. Minimum Height Trees queue: degree为1的顶点 degree[ i ] : 和 i 顶点关联的边数. 先添加整个图,然后BFS删除每一层degree为1的节 ...
- 如何将Excel表批量赋值到ArcGIS属性表
情景再现 现需要将Excel表信息批量赋值(不是挂接)到Shp文件的属性表,两张表的字段.记录数一模一样,至于为什么会出现这样的问题,咱也不敢问,只有想个法子把它搞定! 原始的Excel信息表共57列 ...
- Ubuntu sudo 免密码
sudo visudo 这里默认打开的是 nano 编辑器,不习惯可以把默认编辑器换成 vim, 参考 Ubuntu 修改默认编辑器 test ALL=NOPASSWD: ALL
- ReactNative: 使用View组件创建九宫格
一.简言 初学RN,一切皆新.View组件跟我们iOS中UIView类似,作为一个容器视图使用,它主要负责承载其他的子组件.View组件采用的是FlexBox伸缩盒子布局,通过对它的布局可以影响子组件 ...
- iOS: 本地通知的前后变化(iOS10)
一.介绍 通知和推送是应用程序中很重要的组成部分.本地通知可以为应用程序注册一些定时任务,例如闹钟.定时提醒等.远程推送则更强大,提供了一种通过服务端主动推送消息到客户端的方式,服务端可以更加灵活地 ...
- 【Sublime】Sublime 常用插件
1.sublime设置默认浏览器及打开网页的快捷键设置插件 名称:SideBarEnhancements 地址:https://github.com/titoBouzout/SideBarEnhanc ...
- Vscode 打字特效插件Power Mode安装使用说明
壹 ❀ 引 我记得在17年使用atom编辑器的时候,使用过一款打字特效的插件,只要我们输入代码,代码上方就会有与代码颜色对应的星星效果,今天脑抽突然想起了这个中二插件,搜索了一番成功安装,大致效果如 ...
- 开源的13个Spring Boot 优秀学习项目!超53K星,一网打尽!
Spring Boot 算是目前 Java 领域最火的技术栈了,也是Java开发人员不得不掌握的技术,今天给大家整理了13个优质 Spring Boot 开源项目给大家参考,希望能够帮助到正在学习 S ...
- 基于python的selenium常用操作方法(2)
9 多表单切换 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...