Unique Paths II
这题在Unique Paths的基础上增加了一些obstacle的位置,应该说增加的难度不大,但是写的时候对细节的要求多了很多,比如,第一列的初始化会受到之前行的第一列的结果的制约。另外对第一行的初始化,也要分if else赋值。很容易出现初始化不正确的情况。
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if(obstacleGrid[][]==)
return ;
int m=obstacleGrid.size();
int n=obstacleGrid[].size();
int * row1=new int[n];
int * row2=new int[n];
row1[]=obstacleGrid[][]==?:;
int obs=;//indicate the first row is obscaled
for(int i=;i<n;i++)
{
if(row1[i-]==||obstacleGrid[][i]==)
row1[i]=;
else
row1[i]=;
}
for(int i=;i<m;i++)
{
row2[]=obstacleGrid[i][]==?:;
if(obstacleGrid[i][]==||obs==)
{
obs=;
row2[]=;
} for(int j=;j<n;j++)
{
if(obstacleGrid[i][j]==)
row2[j]=;
else
{
row2[j]=row2[j-]+row1[j];
}
}
row1=row2;
}
return row1[n-];
}
};
Unique Paths II的更多相关文章
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 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 ...
- 61. Unique Paths && Unique Paths II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- [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 —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
随机推荐
- caffe安装:ubuntu16.04 + opencv2.4 + python 2.7+ CUDA 8.0 RC + CuDNN 5.0
官方教程:http://caffe.berkeleyvision.org/install_apt.html 主要参考教程: https://github.com/BVLC/caffe/wiki/Ubu ...
- good design
canvas 效果麦当劳40周年纪念日 效果图 McDonald's 40th Anniversary 交互效果很流畅,赞!
- Fibers in JVM
转载: Fibers in JVM
- h5
1. 在iPhone 手机上默认值是(电话号码显示为拨号的超链接): <meta name="format-detection" content="telephon ...
- RedHat下Bugzilla的安装和配置
Bugzilla 是一个开源的缺陷跟踪系统(Bug-Tracking System). OS:RedHat Linux 软件类型:开源 架构:B/S server端模块开发语言:perl(c/c++) ...
- Sprint1(第三天11.16)
Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:web版-餐厅到店点餐系统 4.团队博客地址: http://www.cnblogs.com/qu ...
- 常用备份工具是mysql自带的mysqldump
常用备份工具是mysql自带的mysqldump,mysqldump -u root -p密码 dbname >d:\test.sql ------------备份某个库mysqldump -u ...
- 字符串0.在php和js中转换为布尔类型 值是false还是true
在php 中 $a = '0'; $b = (bool)$a; var_dump($a);//输出false 在js中官方说明: Note:If the value parameter is omit ...
- Balanced Binary Tree [LeetCode]
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Mongodb 副本集分片(一)---初始化mongodb安装启动
写在前面:mongodb是nosql非关系型数据库中,比较受欢迎的产品.在数据持久化及与关系型数据库的关联上也做的比较好,目前各大公司在存放二进制文件(图片.视频等)中应用也比较广泛.其遵循的key- ...