Unique Paths II (dp题)
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.
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid.size()==) return ;
int row=obstacleGrid.size();
int col=obstacleGrid[].size();
int dp[][];
memset(dp,,sizeof(dp));
bool rblockTag=false;
bool cblockTag=false;
if(obstacleGrid[][]==||obstacleGrid[row-][col-]==)
return ;
for(int i=;i<col;++i){
if(obstacleGrid[][i]==) rblockTag=true;
if(!rblockTag) dp[][i]=;
else dp[][i]=;
}
for(int j=;j<row;++j){
if(obstacleGrid[j][]==) cblockTag=true;
if(!cblockTag) dp[j][]=;
else dp[j][]=;
}
for(int i=;i<row;++i){
for(int j=;j<col;++j){
if(obstacleGrid[i][j]==) dp[i][j]=;
else dp[i][j]=dp[i][j-]+dp[i-][j];//i和j是从1开始
}
}
return dp[row-][col-];
}
};
Unique Paths II (dp题)的更多相关文章
- leetcode-63. Unique Paths II · DP + vector
题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [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 ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- .netcore中使用EFCore连接SQL Server并部署至Ubuntu
前面一篇记录了如何在windows下开发asp.net core程序,并部署至ubuntu系统中.但仅仅是建立了一个demo项目,项目本身并没有实现多少功能.多数时候,我们的项目是要和数据库打交道.E ...
- EXCEL Skills Commonly Used
1. 判断某一列中的数据是否在另一列中 http://jingyan.baidu.com/article/358570f67fd4b0ce4724fc29.html 2. 快速删除excel中的空格( ...
- zuul 整理
网关: 为了解决ip+端口的不友好性而产生.具有服务代理的功能nginx 功能: 1.验证与安全保障: 识别面向各类资源的验证要求并拒绝那些与要求不符的请求. 2.审查与监控: 在边缘位置追踪有意义数 ...
- pip install python-igraph 报错,C core of igraph 没有安装。
(一)问题描述 Centos7 安装python-igraph时,pip install python-igraph 报错,C core of igraph 没有安装. failure: repoda ...
- MFC模态框关闭时出现断言报错!
我尝试一个老的主对话框上创建另一个新的模态对话框并结束对话框,然后包含创建模态对话框的函数体执行结束时,我出现了这个断言失败! 原因:使用PostQuitMessage(1),导致出现上述问题:或者别 ...
- DI:Defect Index(缺陷率)
DI:Defect Index(缺陷率) 定义:DI值是衡量软件质量的高低的指标之一. 公式:DI= 致命级别的问题个数*10+严重级别的问题个数*3+一般级别的问题个数*1+提示级别的问题个数*0. ...
- Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use ...报错
错误截图 Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* t ...
- Django之使用celery异步完成发送验证码
使用celery的目的:将项目中耗时的操作放入一个新的进程实现 1.安装celery pip install celery 2.在项目的文件夹下创建包celery_tasks用于保存celery异步任 ...
- 16. PLUGINS
16. PLUGINS PLUGINS表提供有关服务器插件的信息. PLUGINS表有以下列: PLUGIN_NAME :用于在诸如INSTALL PLUGIN和UNINSTALL PLUGIN之类的 ...
- 02-Mysql中的运算符
Mysql中运算符 1.算术运算符运算符 作用+ 加法- 减法* 乘法/,DIV 除法,返回商%,MOD 除法,返回余数 mysql root@localhost: ...