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 ...
随机推荐
- (转载)MongoDB C#驱动中Query几个方法
MongoDB C#驱动中Query几个方法 Query.All("name", "a", "b");//通过多个元素来匹配数组 Query ...
- 函数式functor的理解
// 参考 // http://jiyinyiyong.github.io/monads-in-pictures/ // https://llh911001.gitbooks.io/mostly-ad ...
- attr和prop区别
今天写了个小程序,获取input[checked]属性,$('#check1').attr('checked'),结果返回undefined,查询了一番, 如果是具有true和false两个属性值的属 ...
- django一对多关系的小例题
urls.py from django.conf.urls import urlfrom django.contrib import adminfrom son1.views import * url ...
- instanceof, isinstance,isAssignableFrom的区别
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false. 形象地:自身实例或子类实例 instanc ...
- 转!!java事务的处理
java的事务处理,如果对数据库进行多次操作,每一次的执行或步骤都是一个事务.如果数据库操作在某一步没有执行或出现异常而导致事务失败,这样有的事务被执行有的就没有被执行,从而就有了事务的回滚,取消先前 ...
- git入门网站
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git入门教程:对商业的.开源的. ...
- hibernate hql
hibernate在使用hql进行select count(*) from ObjectA left join fetch apath 时会报错,多余的left join去掉即可.
- IO流中SequenceInputStream类
SequenceInputStream类: 不断的读取InputStream流对象,对于使用Enumeration对象的情况,该类将持续读取所有InputStream对象中的内容,直到到达最后一个In ...
- ios 常用字符串的操作
//将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8S ...