这题在Unique Paths的基础上增加了一些obstacle的位置,应该说增加的难度不大,但是写的时候对细节的要求多了很多,比如,第一列的初始化会受到之前行的第一列的结果的制约。另外对第一行的初始化,也要分if else赋值。很容易出现初始化不正确的情况。

  代码:

  

  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
  4. if(obstacleGrid[][]==)
  5. return ;
  6. int m=obstacleGrid.size();
  7. int n=obstacleGrid[].size();
  8. int * row1=new int[n];
  9. int * row2=new int[n];
  10. row1[]=obstacleGrid[][]==?:;
  11. int obs=;//indicate the first row is obscaled
  12. for(int i=;i<n;i++)
  13. {
  14. if(row1[i-]==||obstacleGrid[][i]==)
  15. row1[i]=;
  16. else
  17. row1[i]=;
  18. }
  19. for(int i=;i<m;i++)
  20. {
  21. row2[]=obstacleGrid[i][]==?:;
  22. if(obstacleGrid[i][]==||obs==)
  23. {
  24. obs=;
  25. row2[]=;
  26. }
  27.  
  28. for(int j=;j<n;j++)
  29. {
  30. if(obstacleGrid[i][j]==)
  31. row2[j]=;
  32. else
  33. {
  34. row2[j]=row2[j-]+row1[j];
  35. }
  36. }
  37. row1=row2;
  38. }
  39. return row1[n-];
  40. }
  41. };

  

Unique Paths II的更多相关文章

  1. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  2. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  3. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  4. 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 ...

  5. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  6. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  7. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  8. [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 ...

  9. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  10. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

随机推荐

  1. (转载)MongoDB C#驱动中Query几个方法

    MongoDB C#驱动中Query几个方法 Query.All("name", "a", "b");//通过多个元素来匹配数组 Query ...

  2. 函数式functor的理解

    // 参考 // http://jiyinyiyong.github.io/monads-in-pictures/ // https://llh911001.gitbooks.io/mostly-ad ...

  3. attr和prop区别

    今天写了个小程序,获取input[checked]属性,$('#check1').attr('checked'),结果返回undefined,查询了一番, 如果是具有true和false两个属性值的属 ...

  4. django一对多关系的小例题

    urls.py from django.conf.urls import urlfrom django.contrib import adminfrom son1.views import * url ...

  5. instanceof, isinstance,isAssignableFrom的区别

    instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false. 形象地:自身实例或子类实例 instanc ...

  6. 转!!java事务的处理

    java的事务处理,如果对数据库进行多次操作,每一次的执行或步骤都是一个事务.如果数据库操作在某一步没有执行或出现异常而导致事务失败,这样有的事务被执行有的就没有被执行,从而就有了事务的回滚,取消先前 ...

  7. git入门网站

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 git入门教程:对商业的.开源的. ...

  8. hibernate hql

    hibernate在使用hql进行select count(*) from ObjectA left join fetch apath 时会报错,多余的left join去掉即可.

  9. IO流中SequenceInputStream类

    SequenceInputStream类: 不断的读取InputStream流对象,对于使用Enumeration对象的情况,该类将持续读取所有InputStream对象中的内容,直到到达最后一个In ...

  10. ios 常用字符串的操作

    //将NSData转化为NSString        NSString* str = [[NSString alloc] initWithData:response encoding:NSUTF8S ...