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 ...
随机推荐
- PHP获取微信openid 简单教程
//***方法一 获取code https://open.weixin.qq.com/connect/oauth2/authorize?appid=这里是你的公众号的APPID&redirec ...
- 阿里云centos+java环境搭建
目录 .准备 .安装jdk .安装tomcat .安装mysql 1.准备 购买阿里云服务器,我买的是Centos 6.5. 因为是linux,在window下管理我使用XManager,这个软件可以 ...
- MySql安装成功后命令行进行必要的配置
1.1 首次用命令行登录 用zip方式安装成功mysql,并通过net start mysql 命令正常启动mysql服务后,打开dos命令行窗口,输入“mysql -uroot -p”或“mysql ...
- 前端 css续
CSS选择器 1.标签选择器 为类型标签设置样式例如:<div>.<a>.等标签设置一个样式,代码如下: <style> /*标签选择器,找到所有的标签应用以下样式 ...
- R中的运算符,条件语句,控制语句
1.运算符 算术运算符:+,-,*,/ 关系运算符:==,!=,>,>=,<,<= 逻辑运算符:&,|,&&,||,! &和|称为短逻辑符,&a ...
- Myeclipse中Tomcat的两种部署方式
一.在Myeclipse软件中部署 1. 在Myeclipse中,创建好工程后,在Myeclipse菜单栏中选择 Windows -> Preferences -> Myeclipse - ...
- MapReduce-排序(全部排序、辅助排序)
排序 排序是MapReduce的核心技术. 1.准备 示例:按照气温字段对天气数据集排序.由于气温字段是有符号的整数,所以不能将该字段视为Text对象并以字典顺序排序.反之,用顺序文件存储数据,其In ...
- 描述一下你最常用的编程风格---JAVA
描述一下你最常用的编程风格---JAVA 描述一下你最常用的编程风格---JAVA (1)类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都 ...
- java 实现HTTP连接(HTTPClient)
在实习中,使用到了http连接,一直理解的很模糊,特地写个分析整理篇.分析不到位的地方请多多指教. Http 目前通用版本为 http 1.1 . Http连接大致分为2种常用的请求——GET,POS ...
- Android 报错Android - Performing stop of activity that is not resumed
[原文] FROM STACKOVERFLOW: Just giving my 50 cents on the issue. Catching the exception is indeed one ...