Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

  1. [
  2. [0,0,0],
  3. [0,1,0],
  4. [0,0,0]
  5. ]

The total number of unique paths is 2.

Note:m and n will be at most 100.

在Unique Paths 的基础上增加了障碍物。问从起点到终点有多少种走法。

解题思路:

基于上一篇博客,即Unique Paths这道题里的最后一个方法,就是空间复杂度O(min(m,n))的那个解法,接下来的算法的原理就是基于那个的。

因为起点到右边的点路径是唯一的,到下边点路径是唯一的。

所以可以得到一条重要的规律:

起点到终点的路径数等于右边那个点到终点路径数与下边那个点到终点路径数的和。

大概过程是酱紫的:

  1. 用一个长度为n+1的vector对每一列dp 。
  2. 初始化一个长度为n+1,所有值都为0的vector<int> dp
  3. 最开始的时候初始化最后一行,如果不是障碍物,就将dp的对应位置变成1
  4. 然后分别对倒数第二行,倒数第三行……第一行更新dp[i],dp[i] = (mat[m][i] == 1) ? 0 : dp[i] + dp[i+1];
  5. 最后dp中存储的是第一行的每一个非障碍物点到终点的路径数。
  6. dp[0]就是我们的起点啦!~

代码如下:

  1. class Solution {
  2. public:
  3. int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
  4. int m = obstacleGrid.size();
  5. int n = obstacleGrid[].size();
  6. vector<int> dp(n+,);
  7. m--;
  8. int i = n-;
  9. while(i >= && obstacleGrid[m][i] != ){
  10. dp[i] = ;
  11. --i;
  12. }
  13. while(m-- > ){ //这种循环判断可以将只有一行的情况统一考虑进去
  14. for(i = n-; i >= ; i--){
  15. dp[i] = (obstacleGrid[m][i] == )? : dp[i+] + dp[i];
  16. }
  17. }
  18. return dp[];
  19. }
  20. };

【LeetCode练习题】Unique Paths II的更多相关文章

  1. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  2. 【leetcode】Unique Paths II

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

  3. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  4. [LeetCode] 63. Unique Paths II 不同的路径之二

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. leetcode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  7. 【题解】【矩阵】【回溯】【Leetcode】Unique Paths II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  8. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  9. [LeetCode][Java] Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  10. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

随机推荐

  1. VS 项目(c#)引用了 DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称

    1. 在项目上点右键-->属性-->应用程序-->目标框架-->修改为.NET Framework 4. 而我原来的设置是.NET Framework 4 Client Pro ...

  2. 强制IE浏览器或WebBrowser控件使用指定版本显示网页

    自从装了IE10之后,就发现好些个网站显示都不是那么的正常,网站上有些功能竟然还会出现一些意想不到的BUG——本来就是针对IE开发的,现在IE下竟然用不起来了,让用户情何以堪?但是就为少量用户使用的系 ...

  3. 使用WeCloud消息推送接口发送消息NodeJs版

    WeCloud是一家初创公司的产品,眼下主要在做Android和IOS消息推送这块.他们提供了用于向设备发送消息的协议,详细协议内容见消息推送协议. 这篇文章将使用NodeJs基于这个推送协议完毕向A ...

  4. arcengine 调用arctoolbox功能的举例 spatialJoin

    废话不多说,code是王道. 其中str1.str2两个参数是target路径.join路径 private void spatialJoin(Geoprocessor gp, string str1 ...

  5. Android应用切换皮肤功能实现

    原文地址:http://www.eoeandroid.com/thread-318159-1-1.html 现在大多数android应用都支持切换皮肤的功能.比如千千静听,墨迹天气等等.本文介绍两种切 ...

  6. JQuery hover(over,out) 使用笔记

    转载自:http://www.douban.com/note/202404884/ JQuery hover(over,out) 使用笔记 JavaScript 下.onmouseover() 和 o ...

  7. 模板页 相对路径 JS 加载问题

    问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...

  8. int.TryParse 与 int.Parse 的区别

    int.TryParse 与 int.Parse 的区别是,int.TryParse不会产生异常,转换成功返回 true,转换失败返回 false.最后一个参数为输出值,如果转换失败,输出值为 0. ...

  9. VC++ 控制台不自动退出

    1.Ctrl+F5 2.结尾添加 getchar() 3.结尾添加 system("pause"); 参考:http://jingyan.baidu.com/article/555 ...

  10. I/O多路复用之epoll

    1.select.poll的些许缺点 先回忆下select和poll的接口 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set ...