题目:

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.

题解:

Solution 1 ()(未优化)

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if(obstacleGrid.empty() || obstacleGrid[].empty()) return ;
int m = obstacleGrid.size(), n = obstacleGrid[].size();
vector<int> dp(n,);
dp[] = ;
for(int i=; i<m; ++i) {
for(int j=; j<n; ++j) {
if(obstacleGrid[i][j] == )
dp[j] = dp[j-];
else dp[j] += dp[j-];
}
}
return dp[n-];
}
};

Solution 2 ()

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if(obstacleGrid.empty() || obstacleGrid[].empty()) return ;
int m = obstacleGrid.size(), n = obstacleGrid[].size();
vector<int> dp(n,);
dp[] = ;
for(int i=; i<m; ++i) {
for(int j=; j<n; ++j) {
if(obstacleGrid[i][j] == )
dp[j] = ;
else {
if(j > )
dp[j] += dp[j-];
}
}
}
return dp[n-];
}
};

【LeetCode】063. Unique Paths II的更多相关文章

  1. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  2. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  3. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  4. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  5. 【LeetCode】62. Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  6. 【LeetCode】062. Unique Paths

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

  7. 【LeetCode】980. Unique Paths III解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. 【leetcode】980. Unique Paths III

    题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  Ther ...

  9. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. UbuntuServer12.04安装MongoDB,开机自启,服务,权限

    获取最新版本 去http://www.mongodb.org/downloads找最新版的链接 wget http://fastdl.mongodb.org/linux/mongodb-linux-x ...

  2. java自定义before和after

    package com.ada.wuliu.worker.web.cooperation.worker; public class TestOne { abstract class Father{ p ...

  3. Android----SharedPreferences(存储数据)

    SharedPreferences详解 我们在开发软件的时候,常需要向用户提供软件参数设置功能,例如我们常用的微信,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是在windo ...

  4. HDFS源码分析数据块复制之PendingReplicationBlocks

    PendingReplicationBlocks实现了所有正在复制的数据块的记账工作.它实现以下三个主要功能: 1.记录此时正在复制的块: 2.一种对复制请求进行跟踪的粗粒度计时器: 3.一个定期识别 ...

  5. 安装部署Solrcloud

    实验说明: 三台虚拟机做solrcloud集群                            安装solr前请确保jdk .tomcat.zookeeper已安装好,否则无法启动 三台虚拟机I ...

  6. [转]screen 的使用

    当我们使用securecrt,putty, 等连接远程服务器时,如果正在执行一个程序,比如shell 脚本,退出当前的窗口会导致程序终止!其原理如下: 根据POSIX.1定义: 1 挂断信号(SIGH ...

  7. 深入Asyncio(十一)优雅地开始与结束

    Startup and Shutdown Graceful 大部分基于asyncio的程序都是需要长期运行.基于网络的应用,处理这种应用的正确开启与关闭存在惊人的复杂性. 开启相对来说更简单点,常规做 ...

  8. 史上最浅显易懂的Git教程3 分支管理

    假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了.如果等代码全部写完再一次提交,又存在丢失每天进度的巨大风险 ...

  9. 如何将linux服务器作为文件服务器

    在开发过程中想要使用linux服务器作为文件服务器,可以通过 IP+文件名来获取文件信息,比如http://localhost/banner/a.jpg.设置过程如下 1.安装apache2 sudo ...

  10. 以python理解Linux的IO多路复用,select、poll、epoll

    题外话 之前在看Unix环境高级编程的时候,看完高级IO那一章,感觉自己萌萌哒,0.0 ,有点囫囵吞枣的感觉,之后翻了几篇博客,从纯系统的角度理解,稍微有了点概念,以这两篇为例,可以以后参考: htt ...