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 ...
随机推荐
- 【Nginx】HTTP请求的11个处理阶段
Nginx将一个HTTP请求分成多个阶段.以模块为单位进行处理.这样做的优点是使处理过程更加灵活.减少耦合度.HTTP框架将处理分成了11个阶段,各个阶段能够包括随意多个HTTP模块并以流水线的方式处 ...
- Linux:Ubuntu16.04下创建Wifi热点
Linux:Ubuntu16.04下创建Wifi热点说明 1.Ubuntu16.04里面可以直接创建热点,而不用像以前的版本,还要其他辅助工具. 2.本篇文章参考自编程人生 具体步骤如下: 1. 点击 ...
- str字符串、bool类型常用方法总结
字符串拼接 必须是字符串与字符串拼接 print('马化腾'+'马云') print('马化腾' * 10) 将打印10个马化腾 字符串翻转 [ : :-1] 字符串可以加和乘,不能减和乘 input ...
- $2015 武汉森果公司web后端开发实习日记----书写是为了更好的思考
找暑期实习,3月份分别投了百度和腾讯的实习简历,都止步于笔试,总结的主要原因有两点:基础知识不扎实,缺乏项目经验.后来到拉勾网等网站上寻找实习,看了很多家,都还是处于观望状态.后来参加了武汉实习吧在大 ...
- java Excel导入导出工具类
本文章,导入导出依赖提前定义好的模板 package com.shareworx.yjwy.utils; import java.io.File; import java.io.FileInputSt ...
- node做验证码
使用了ccap插件 1.安装: 通用方法:npm install ccap 2. cnst ccap= require('ccap')({ width: 128, height: 40, offset ...
- for update排他锁详解
使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...
- 七 、linux正则表达式
为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...
- Python 数值类型
1.数值类型分为整形(二进制(0b),八进制(0o),十进制,十六进制(0x) ),浮点型,long,complex(复合行) 当我们说十进制数的时候,是逢10进1,就是说到达10的时候就要向前一位进 ...
- springmvc文件上传的基本描述
SpringMVC的文件上传,底层也是使用的Apache的Commons-fileupload 可以分为三步: 1.导入依赖包 <!-- 文件上传的依赖 --> <dependenc ...