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.

class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int m=obstacleGrid.size();
int n=obstacleGrid[0].size(); vector<vector<int>> paths(m,vector<int>(n,0));
if(obstacleGrid.at(0).at(0)==1)
{
return 0;
}
else{
paths.at(0).at(0)=1;
} for(int i=1;i<m;i++)
{
if(obstacleGrid.at(i).at(0)==0 && paths.at(i-1).at(0)==1)
paths.at(i).at(0)=1;
}
for(int i=1;i<n;i++)
{
if(obstacleGrid.at(0).at(i)==0 && paths.at(0).at(i-1)==1)
paths.at(0).at(i)=1;
} for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
if(obstacleGrid.at(i).at(j)==1)
paths.at(i).at(j)=0;
else
paths.at(i).at(j)=paths.at(i).at(j-1)+paths.at(i-1).at(j);
}
} return paths.at(m-1).at(n-1);
}
};

[LeetCode] Unique Paths 2的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

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

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

  3. [LeetCode] Unique Paths 不同的路径

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

  4. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [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 ...

  8. Leetcode Unique Paths II

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

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  10. Leetcode Unique Paths

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

随机推荐

  1. 发送邮件给某人:mailto标签

    mailto标签 1.标签最简式 <a href="mailto:xxx@xx.com">联系站长</a> 2.标签帮你填抄送地址 <a href=& ...

  2. HDU2191:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

    Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品 ...

  3. Vmware虚拟机下三种网络模式配置

    VMware虚拟机有三种网络模式,分别是Bridged(桥接模式).NAT(网络地址转换模式).Host-only(主机模式). VMware workstation安装好之后会多出两个网络连接,分别 ...

  4. 03-OC实例方法、内存管理

    目录: 一.实例方法和类方法 二.工厂方法 三.self 四.编程规范 五.内存管理 回到顶部 一.实例方法和类方法 1 只能通过实例调用的方法叫实例方法."-" 2 只能通过类调 ...

  5. 一、Mongo的安装

    注:学习为主,平台为WIN7 32位系统 一.Mongo的安装 1.1 下载 到官方下载地址(http://www.mongodb.org/downloads)去下载所需要的版本 1.2 安装与运行 ...

  6. set global read_only=0; 关闭只读,可以读写 set global read_only=1; 开始只读模式

    mysql> set global read_only=0; Query OK, 0 rows affected (0.00 sec) mysql> show variables like ...

  7. GDOI2015——已成梦

    今年GDOI(2015)在韶关北江中学(没记错的话应该是武江区)举行,感觉这五天就是一场梦,一场包含苦辣的梦. Day0 坐了一个上午的车,而且车内的空气又不好,感觉整个人都累倒下了. 到了北江之后吃 ...

  8. Android圆环形颜色选择器:HoloColorPicker

    HoloColorPicker实现圆环形颜色选择器,可以改变颜色饱和度来选择颜色.选择颜色时,可以用手指沿着圆环滑动一个滑块,从而选择颜色. 添加以下XML至你的布局中: ? 1 2 3 4 < ...

  9. javascript每日一练(三)——DOM一

    一.Dom基础 childNodes(有兼容问题),children nodeType getAttribute() firstChild,lastChild,previousSilbing,next ...

  10. Android-x86 4.4-r5 发布,PC 上的安卓系统

    Android x86 即运行于 x86 PC上的Android操作系统,目前已经支持大部分安卓程序. Android X86平台是由Beyounn和Cwhuang主持设计的.项目的主要目的在于为X8 ...