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. python高级编程之(类级):子类内建类型

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...

  2. HTTP 协议实现

    一.超文本传输协议及HTTP包    HTTP协议用于在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议--客户端发送一个请求,服务器返回该请求的应答,所有的请求与应答都是HTT ...

  3. 关于safari上的select宽高问题小技,自定义下拉框

    之前一直用windows做开发,最近换了个mac,在几经折腾之下,安装完了各种开发工具,IDE等,然后欣然打开自己正在开发的网站.突然发现mac上所有的下拉框都变了,都是默认样式,无论padding, ...

  4. freemarker报错之三

    1.错误描写叙述 Expression students is undefined on line 30, column 24 in student.ftl. The problematic inst ...

  5. Android开发之去掉标题栏的三种方法,推荐第三种

    Android:去掉标题栏的三种方法和全屏的三种方法 第一种:一般入门的时候常常使用的一种方法 onCreate函数中增加下面代码: requestWindowFeature(Window.FEATU ...

  6. markdown 书写代码

    近期基于github + hexo 搭建了自己的博客.開始用markdown写博客,推荐 mac 平台用 mou 这个软件或者 vim. 介绍下markdown语法插入代码的规则: 有一种方法是全部代 ...

  7. Golang性能调优入门

    如何利用golang自带的profile工具进行应用程序的性能调优,前一段时间我做的日志分析系统在线上遇到了一个问题,就是分任务的系统down机了,日志处理延迟了10几个小时,这个时候任务分发系统重启 ...

  8. Canvas Api简介1

    canvas canvas 其实对于HTML来说很简单,只是一个标签元素而已,自己并没有行为,但却把一个绘图 API 展现给客户端 JavaScript 以使脚本能够把想绘制的东西都绘制到一块画布上, ...

  9. Android adb不是内部或外部命令 (转)

    dos窗口运行adb命令出现错误:adb不是内部或外部命令…. 出现问题原因及解决办法: 1.没有配置相关环境变量. 只要将android 的sdk安装路径添加到系统变量Path中即可. (以win7 ...

  10. C# 仿百度自动匹配

    private void Form1_Load(object sender, EventArgs e) { AutoCompleteStringCollection source = new Auto ...