这题在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的更多相关文章

  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. SpringMVC生成任意文件,访问链接即下载

    原理上讲就是返回的 ResponseEntity<byte[]> 形式的值就可以了 @RequestMapping("/api/watermark_download") ...

  2. git备份sublime插件及配置

    github备份sublime配置 sublime使用的时间长了,渐渐的就积累了一些有用甚至离不开的插件.但是有时候系统会出点问题,或者换电脑什么的,这时候要想在找回那个曾经的sublime就不那么容 ...

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

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

  4. freemarker数字格式化

    1.在模板中直接加.toString()转化数字为字符串,如:${languageList.id.toString()}: 2.在freemarker配置文件freemarker.properties ...

  5. VS中Qt的探索02

    边看C++ GUI QT4教程,边在VS2010中进行编程学习探索. 在使用Qt设计师时,其中每一个对象的ObjectName属性是非常重要的,在程序功能的实现过程中,需要不断的使用该变量名. 当所有 ...

  6. Rigidbody相关的操作最好放在FixedUpdate中,update中可能会无效果

    void Turning() { // Create a ray from the mouse cursor on screen in the direction of the camera. Ray ...

  7. 深入理解javascript系列,读书笔记

    深入理解JavaScript系列(2):揭秘命名函数表达式 1.讲了函数声明和函数表达式的区别,包括一些在函数提升上的区别 2.如果给函数表达式的函数也取名,会在调试的时候受益 3.不要在block( ...

  8. VNC SERVER配置

    vnc的配置网上有很多 普通用户的配置没有怎么写 根据下面这个说法 https://www.digitalocean.com/community/tutorials/how-to-install-an ...

  9. mybatis中 ${}和#取值小记(Parameter index out of range)

    mybatis mapperxml文件中有两种取值法.${}和#{} $的是原样,#的是取值并转成指定?#{ele1,jdbcType=VARCHAR} 有个坑, 错误的写法 <if test= ...

  10. 关于ViewPager、ViewFilpper、ViewFlow三种实现水平向滑动方式的比较

    ViewPagerViewPager类提供了多界面切换的新效果.新效果有如下特征:[1] 当前显示一组界面中的其中一个界面.[2] 当用户通过左右滑动界面时,当前的屏幕显示当前界面和下一个界面的一部分 ...