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. Centos6.5搭建bugzilla

    一.安装httpd. mod_ssl. mysql-server . mysql .php-mysql . gcc . perl* . mod-perl-devel [root@localhost ~ ...

  2. 二、Mongo命令初识

    简单介绍mongo的一些基本命令 1.   连接与登陆mongo 在命令行输入“mongo”命令即可登陆Mongo数据库(PS:默认讨论被信任的环境,也就是不需要用户名和密码进行登陆). 查看当前所使 ...

  3. iOS开发之使用Ad Hoc进行测试

    由于最近某个项目需要给别人测试,使用的是Ad Hoc方法 首先登录开发者官网配置证书 1.添加Certificates,从电脑获取certSigningRequest然后添加进去 2.在Identif ...

  4. Convert SVG to PNG in Python - Stack Overflow

    Convert SVG to PNG in Python - Stack Overflow Convert SVG to PNG in Python

  5. Oracle undo 镜像数据探究

                                                                 Oracle undo 镜像数据探究  今天是2013-08-18,隔别一周的 ...

  6. windows下apache如何完整卸载?

    1.运行services.msc,在服务中停止 apache 服务.2.运行命令行程序,输入 sc delete apache,删除该服务3.删除apache文件夹.

  7. 一道经典的C++结构体的题目

    题目描述: 有10个学生,每个学生的数据包括学号.姓名.英语.数学.物理三门课的成绩,从键盘输入10个学生数据,要求打印出3门课程的总平均成绩,以及最高分的学生的数据(包括学号,姓名,3门课的平均成绩 ...

  8. 关于js基础easy忘记的那些事儿

    1.Number() 通过这个函数转化后的值仅仅有两个:数值和NaN,通过parseInt也能转化为数值.可是像"134df"转化后的值为134,而Number("134 ...

  9. windows api 梳理

    PathMatchSpec Function Searches a string using a Microsoft MS-DOS wild card match type. Syntax BOOL  ...

  10. 微软C#版SQLHelper.cs类

    转载自:http://blog.csdn.net/fengqingtao2008/article/details/17399247 using System; using System.Data; u ...