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.

[
[0,0,0],
[0,1,0],
[0,0,0]
]

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]就是我们的起点啦!~

代码如下:

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
vector<int> dp(n+,);
m--;
int i = n-;
while(i >= && obstacleGrid[m][i] != ){
dp[i] = ;
--i;
}
while(m-- > ){ //这种循环判断可以将只有一行的情况统一考虑进去
for(i = n-; i >= ; i--){
dp[i] = (obstacleGrid[m][i] == )? : dp[i+] + dp[i];
}
}
return dp[];
}
};

【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. batch 批处理获取系统时间

    文件test.bat,内容命令如下: @echo off set filename=%,%-%,%-%,% %,%:%,%:%,% echo %filename% pause

  2. 分析linux下的编译环境

    不论是windows下的程序,还是linux下的程序,开发环境都离不开三个目录:include.lib.bin,分别是头文件目录.库文件目录.运行文件目录.或许目录不叫这个名字,但却必不可少,除非你的 ...

  3. C++ Primer笔记1_转义字符_标准库类型string_标准库类型vector

    1.转义字符 一般有两种方式: \x后紧跟1个或多个十六进制数字.或\后紧跟1.2.3个八进制数字,当中数字部分是字符相应的数值. #include <iostream> using na ...

  4. freemarker书写select组件错误摘要(七)

    1.错误叙述性说明 六月 26, 2014 11:26:27 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...

  5. Android应用程序内部启动Activity过程(startActivity)的源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6703247 上文介绍了Android应用程序的 ...

  6. EffectiveC#6--区别值类型数据和引用类型数据

    1. 设计一个类型时,选择struct或者class是件简单的小事情,但是,一但你的类型发生了改变, 对所有使用了该类型的用户进行更新却要付出(比设计时)多得多的工作. 2.值类型:无多态但性能佳. ...

  7. MVC自我学起之MVCMusic开发中遇到问题:musicstore edit方法出错的原因和解决方法

    错误提示: 存储区更新.插入或删除语句影响到了意外的行数(0).实体在加载后可能被修改或删除.刷新 ObjectStateManager 项. 解决案: 1.在view中或model中增加隐藏id 1 ...

  8. Cocoapods依赖管理

    对于iOS App的开发,几乎都采用了Cocoapods来管理第三方库,那么对于开发人员来说,这是必备技能,必须要掌握如何使用.这篇文章就是介绍如何安装和使用CocoaPods的. 简单来说,就是专门 ...

  9. 修改 “嗨加游-Prefix.pch” 或者 “嗨加游-Info.plist ” 方法

    如果要更改Info.plist与Prefix.pch文件实际路径,也就是实际文件的位置(不是在工程中的组织路径),需要到Build Settings中修改对应的配置,不然工程就找不到对应的Info.p ...

  10. Link-Cut-Tree题目泛做(为了对应自己的课件)

    题目1:BZOJ 2049 洞穴勘测 #include <bits/stdc++.h> #define L(x) c[x][0] #define R(x) c[x][1] using na ...