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 ...
随机推荐
- Mybatis----传入参数parameterType类型详解
Mybatis----传入参数parameterType类型详解 前言 Mybatis的Mapper文件中的select.insert.update.delete元素中有一个parameterType ...
- python爬虫之csv文件
一.二维数据写入csv文件 题目要求: 读入price2016.csv文件,将其中的数据读出,将数字部分计算百分比后输出到price2016out.csv文件中 知识点: 对于列表中存储的二维数据, ...
- ES6 class类中定义私有变量
ES6 class类中定义私有变量 class类的不足 看起来, es6 中 class 的出现拉近了 JS 和传统 OOP 语言的距离.但是,它仅仅是一个语法糖罢了,不能实现传统 OOP 语言一样的 ...
- Windows10 下利用Hyper-V安装CentOS系统
开启Windows10的Hyper-v功能(需要重启电脑) 控制面板→程序→启用或关闭Windows功能→打开Hyper-v→确定 创建虚拟机 在Windows管理工具中找到Hyper-v管理器并双击 ...
- MySQL数据库实战之优酷
目录 一.项目总结三步骤 二.项目需求分析 三.搭建框架 四.ORM框架分析 五.数据库设计 六.项目中各个功能模块分析 七.项目中遇到的问题及怎么解决的 八.客户端代码框架 8.1 conf 8.1 ...
- 学习workerman之前需要知道的几种php回调写法
在workerman中会经常使用,我们先写一个回调函数,当某个行为被触发后使用该函数处理相关逻辑. 在PHP中最常用的几种回调写法如下 匿名函数做为回调 匿名函数(Anonymous function ...
- SqlServer PIVOT行转列
PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合. 测试数据 INSERT INTO [TestRows2Columns] ...
- jsp模板
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+re ...
- F5的作用
F5 F5的全称是F5-BIG-IP-GTM,是最流行的硬件负载均衡设备,其并发能力达到百万级.F5的主要特性包括: 多链路的负载均衡和冗余 可以接入多条ISP链路,在链路之间实现负载均衡和高可用. ...
- 易优CMS:小白学代码之notempty
[基础用法] 名称:notempty 功能:判断某个变量是否为空,可以嵌套到任何标签里面使用,比如:channel.type等 语法: {eyou:notempty name='$eyou.field ...