[Leetcode] unique paths 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 as1and0respectively 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 is2.
Note: m and n will be at most 100.
题意:增加障碍,不能到达障碍,不能越过障碍。
思路:思路和unique paths是一样的,只是要判断当前值是否为1,若是,则其对应的dp数组中赋值为0,不是时,状态方程是:dp[i][j]=dp[i-1][j]+dp[i][j-1]。代码如下:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
{
int m=obstacleGrid.size(),n=obstacleGrid[].size();
vector<vector<int>> dp(m,vector<int>(n,));
if(m==||n==) return ;
if(obstacleGrid[][]==) return ;
dp[][]=; //初始行
for(int i=;i<m;++i)
{
if(obstacleGrid[i][] ==)
{
break;
}
else
dp[i][]=;
}
//初始列
for(int i=;i<n;++i)
{
if(obstacleGrid[][i] ==)
{
break;
}
else
dp[][i]=;
} for(int i=;i<m;++i)
{
for(int j=;j<n;++j)
{
if(obstacleGrid[i][j] !=)
dp[i][j]=dp[i-][j]+dp[i][j-];
}
} return dp[m-][n-];
}
};
当用一维数组去简化时,要注意些问题,仅一行,或者仅一列时,还要依次的确定数组dp[]中对应的值,不是像上一题那样简单赋值为1就行,所以,遍历时,行和列的起始点都是从0开始;另外也得注意的是,数组dp中的当前的前一个是否存在,即, j >0。代码如下:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid)
{
int row=obstacleGrid.size(),col=obstacleGrid[].size();
if(obstacleGrid[][]==) return ;
vector<int> dp(col,);
dp[]=; for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(obstacleGrid[i][j]==)
dp[j]=;
else if(j>)
dp[j]+=dp[j-];
}
}
return dp[col-];
}
};
[Leetcode] unique paths ii 独特路径的更多相关文章
- [LeetCode] 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 —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 063 Unique Paths II 不同路径 II
这是“不同路径” 的进阶问题:现在考虑网格中有障碍物.那样将会有多少条不同的路径从左上角到右下角?网格中的障碍物和空位置分别用 1 和 0 来表示.例如,如下所示在 3x3 的网格中有一个障碍物.[ ...
随机推荐
- TP3.2.3 接入阿里sms 短信接口
阿里云短信接口 配置文件 config.php //阿里大鱼 'Ali_SMS' =>array( 'sms_temp' =>'短信模板', 'sms_sign' =>'签名', ' ...
- 总结Verilog中always语句的使用
always语句包括的所有行为语句构成了一个always语句块.该always语句块从仿真0时刻开始执行其中的行为语句:最后一条执行完成后,再开始执行其中的第一条语句,如此往复循环,直到整个仿真结束. ...
- ORACLE中order by造成分页不正确原因分析
工作中遇到的问题: 为调用方提供一个分页接口时,调用方一直反应有部分数据取不到,且取到的数据有重复的内容,于是我按以下步骤排查了下错误. 1.检查分页页码生成规则是否正确. 2.检查SQL语句是否正 ...
- set<pair<int,int> > 的运用
In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to ...
- 一步一步学Linq to sql(六):探究特性
延迟执行 IQueryable query = from c in ctx.Customers select c; 这样的查询句法不会导致语句立即执行,它仅仅是一个描述,对应一个SQL.仅仅在需要使用 ...
- C++11中std::bind的使用
std::bind: Each argument may either be bound to a value or be a placeholder: (1).If bound to a value ...
- Java:位移运算符
Java中有三个位移运算符,用于对int类型整数的二进制补码进行操作: 1. "<<": 左移运算符 在二进制补码末尾添加“0”,之前的其他位相当于左移了一位,可看作成 ...
- Delphi实例之绘制正弦函数图像
Delphi实例之绘制正弦函数图像 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...
- android activity状态保存
一.被其他任务打断(来电话),再次打开希望保留数据 private String SAVE_INSTANCE_TAG = "ATWAL"; @Override protected ...
- (原)Android到IOS开发的转换(一)
序)闲扯几句 很早就想入手ios开发,但是一直没有机会,个人没有水果机器,上个公司上班的那台mac mini虽然就在我身边,灰都有一层了,但是一直没有机会开机学习下,因为事多,自上一篇文章后,离职后, ...