LeetCode OJ:Unique Paths II(唯一路径II)
Follow up for "Unique Paths":
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.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
这个与前面那个题目基本上差不多,具体就是多了障碍,实际上遇到障碍的话把到该点的路径的数目置为0就可以了,其他的基本上与前面相同:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if(obstacleGrid.size() == || obstacleGrid[].size() == ) return ;
vector<vector<int>> res(obstacleGrid.size(), vector<int>(obstacleGrid[].size(), ));
int maxHor = obstacleGrid.size();
int maxVer = obstacleGrid[].size();
res[][] = obstacleGrid[][] == ? : ;
for(int i = ; i < maxHor; ++i){
res[i][] = obstacleGrid[i][] == ? : res[i - ][];
}
for(int j = ; j < maxVer; ++j){
res[][j] = obstacleGrid[][j] == ? : res[][j - ];
}
for(int i = ; i < maxHor; ++i){
for(int j = ; j < maxVer; ++j){
res[i][j] = obstacleGrid[i][j] == ? : res[i - ][j] + res[i][j - ];
}
}
return res[maxHor - ][maxVer - ];
}
};
java版本的代码如下所示:
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0)
return 0;
int [][] grid = new int [obstacleGrid.length][obstacleGrid[0].length];
grid[0][0] = obstacleGrid[0][0]==1 ? 0 : 1;
for(int i = 1; i < obstacleGrid.length; ++i){
grid[i][0] = obstacleGrid[i][0]==1? 0 : grid[i-1][0];
}
for(int i = 1; i < obstacleGrid[0].length; ++i){
grid[0][i] = obstacleGrid[0][i]==1? 0 : grid[0][i-1];
}
for(int i = 1; i < obstacleGrid.length; ++i){
for(int j = 1; j < obstacleGrid[0].length; ++j){
grid[i][j] = obstacleGrid[i][j]==1? 0 : (grid[i-1][j] + grid[i][j-1]);
}
}
return grid[obstacleGrid.length-1][obstacleGrid[0].length-1];
}
}
LeetCode OJ:Unique Paths II(唯一路径II)的更多相关文章
- LeetCode OJ: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] 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不同路径 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] 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):不同路径 II
Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- [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 ...
- C#LeetCode刷题之#63-不同路径 II(Unique Paths II)
目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3682 访问. 一个机器人位于一个 m x ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- Ubuntu Server 16.04安装xfce4图形界面远程控制
1.首先连接上你的服务器,然后安装vncserver,命令如下 apt-get install vnc4server 2.安装图形界面 apt-get install xfce4如果安装不上,就 ap ...
- 交叉熵(Cross-Entropy) [转载]
交叉熵(Cross-Entropy) 交叉熵是一个在ML领域经常会被提到的名词.在这篇文章里将对这个概念进行详细的分析. 1.什么是信息量? 假设X是一个离散型随机变量,其取值集合为X,概率分布函数为 ...
- 1.5 使用电脑测试MC20的发送英文短信功能
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- Python基础(19)_异常处理
一.异常处理 错误和异常: 1.错误的种类: 1)语法错误:这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正 2)逻辑错误: 例如: res1=1/0 .es2=1+'str ...
- grep命令详细解析 --非原创 原作者ggjucheng
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- 前端之 Ajax(补)
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- iOS 学习如何声明私有变量和私有方法
私有变量 首先来说 OC 中没有绝对的私有变量,这么说基于两点原因: 1可修改: 通过KVC 键值编码 来修改私有成员变量的值 2可读取 : 通过底层runtime 获取实例变量Ivar 对应 ...
- SM3算法
/* * sm3.h * * 为使此算法兼容32位.64位下Linux或Windows系统, * 选择 int 来表示 32 位整数. * 消息长度最大限定为 2**32 - 1(单位:比特), * ...
- 04_Apache Hadoop 生态系统
内容提纲: 1)对 Apache Hadoop 生态系统的认识(Hadoop 1.x 和 Hadoop 2.x) 2) Apache Hadoop 1.x 框架架构原理的初步认识 3) Apache ...
- debian内核代码执行流程(三)
接续<debian内核代码执行流程(二)>未完成部分 下面这行输出信息是启动udevd进程产生的输出信息: [ ]: starting version 175是udevd的版本号. 根据& ...