Unique Paths II [LeetCode]
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]
]
The total number of unique paths is 2
.
Note: m and n will be at most 100.
Solution: The same to Unique Paths, just add obstacle check.
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.size() <= || obstacleGrid[].size() <= )
return ; int row = obstacleGrid.size();
int column = obstacleGrid[].size();
vector<int> path_nums(column, );
for(int i = ; i < row; i ++) {
int row_idx = row - - i;
for(int j = ; j < column; j ++ ) {
int column_idx = column - - j;
if(i == && j == && obstacleGrid[row_idx][column_idx] == )
return ;
if(i == && j == && obstacleGrid[row_idx][column_idx] == ) {
path_nums[j] = ;
}else {
if(obstacleGrid[row_idx][column_idx] == ) {
path_nums[j] = ;
}else {
if(j != )
path_nums[j] += path_nums[j - ];
}
}
}
}
return path_nums[column - ];
}
};
Unique Paths II [LeetCode]的更多相关文章
- Unique Paths II ——LeetCode
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- struts2--表单标签
struts2的表单标签可分为两类:form标签本身和包装HTML表单元素的其他标签.form标签本身的行为不同于它内部的元素. struts2表单标签包括: form.textfield.passw ...
- jquery初涉,First Blood
jquery可以帮助干的事情有: 遍历HTML文档 操作DOM 处理事件 执行动画 开发Ajax操作 优点就不在这儿扯蛋了~ 1.jquery环境配置 jquery不需要安装,只需要将下载的jquer ...
- 没有Path的Binding
当Binding源本身就是数据且不需要Path来指明时,可以设置Path的值为".",或直接省略Path.XAML中这个"."可以省略不写,但在C#代码中是不能 ...
- cocos2d-x 3.X (二)创建动起来的精灵
[参考文章]http://www.cnblogs.com/suguoqiang/archive/2013/04/03/2997316.html 在HelloWorldScene.h中声明void ro ...
- 基于Linux的oracle数据库管理 part6 (backup 相关的脚本)
这里只是简单的介绍几种 备份方法 备份: 逻辑备份, 冷备份, 热备份 逻辑备份 也称作 导入(import), 导出(export), 作用是在不同的oracle数据库之间转移数据 物理备份, 就是 ...
- poj1971Parallelogram Counting
链接 越来越感觉到了数学的重要性!.. 这题本来用以斜率和长度为key值进行hash不过感觉很麻烦还TLE了.. 最后知道中点一样的话就可以组成平行四边形,初中数学就可以了.. #include &l ...
- js实现checkbox的全选和全不选功能
html代码: <form name="form1" method="post" action="manage.php?act=sub" ...
- Javascript设计模式之创建对象的灵活性
传统的 /* Anim class */ var Anim = function () {}; Anim.prototype.start = function () { console.log(&qu ...
- Python变量类型
Python变量类型 变量是存储在内存中的值,因此在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定的内存,并决定什么数据可以被存储在内存中. 因此变量可以指定不同的数据类型, ...
- tomcat字符,文档,数据库配置
修改tomcat目录下conf目录下的server.xml tomcat容器的解码配置 URIEncoding="UTF-8" <Connector port="8 ...