这题在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. jQuery 查找父元素

    function deletesec1Div5(obj){ $(obj).closest(".sec1-div5").remove();}自己写的一段代码,实现了table中的全选 ...

  2. linux下使用fork,exec,waitpid模拟system函数

    代码如下: #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include &l ...

  3. iOS开发 二维码生成

    基于libqrencode的二维码生成 + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size { ...

  4. 用流来读取文件(getline,istringstream)

    ifstream infile("fileanme"); 原型:getline(istream &infile, string &line); 函数说明:读取文件中 ...

  5. java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterEncodingFilter

    今天在用git merge 新代码后报了如下错误:java.lang.ClassNotFoundException: org.springframework.web.filter.CharacterE ...

  6. leetcode刷题全纪录(持续更新)

    2.Add Two Numbers 原题链接https://leetcode.com/problems/add-two-numbers/ AC解: public ListNode addTwoNumb ...

  7. 微信内嵌浏览器sessionid丢失问题,nginx ip_hash将所有请求转发到一台机器

    现象微信中打开网页,图形验证码填写后,经常提示错误,即使填写正确也会提示错误,并且是间歇性出现. 系统前期,用户使用主要集中在pc浏览器中,一直没有出现这样的问题.近期有部分用户是在微信中访问的,才出 ...

  8. xampp 端口冲突

    最近使用xampp ,提示端口有问题,使用xampp自带的 xampp control修改的端口之后还是不行. 如果是apache端口有问题就修改 xampp\apache\conf\ httpd.c ...

  9. 本地测试Sql

    数据库sqlserver2008 编程vs2008,C# 该测试是数据库和程序在一台计算机上,如果不在一台计算机上就不一样了 我的数据库最大链接是127,好像不固定. 一.测试链接对速度的影响 sta ...

  10. 前端优化之图片延迟加载(lazyload.js)

    要想缩短首屏加载时间,思路一般是减少http请求次数和降低每次的请求量.本文中使用现成的lazyload.js插件,文末会放出下载地址. lazyload.js可以实现图片分批次加载,不是一次性加载完 ...