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.

与上题差别不大,只需要判断有障碍置零即可。

对于首行首列,第一个障碍及之后的路径数均为0

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

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

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

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

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

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

  3. 【LeetCode】063. Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  4. LeetCode OJ 63. Unique Paths II

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

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

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

  6. [leetcode DP]63. Unique Paths II

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

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

  8. 【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). ...

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

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

随机推荐

  1. CDOJ 842 天下归晋 树状数组

    天下归晋 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/842 Descrip ...

  2. Codeforces Round #303 (Div. 2) B. Equidistant String 水题

    B. Equidistant String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  3. hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

    DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  4. iOS 本地消息推送机制

    转发自:https://www.jianshu.com/p/e347f999ed95     //已经废除的 http://blog.csdn.net/three_zhang/article/deta ...

  5. tortoise git常用功能

    1.打tag TortoiseGit -> show log -> 选中版本 -> create tag at this version... TortoiseGit -> p ...

  6. JLink Support for the Nuvoton NUC1xx devices

    http://forum.segger.com/index.php?page=Thread&threadID=1441 Friday, April 19th 2013, 7:25pm Hi M ...

  7. rt-80启动tomcat

    1 #!/bin/sh 2 cd /mnt/tomcat/tomcat_8082; 3 ps -ef|grep /tomcat_8082/ |awk '{print $2}'|xargs kill - ...

  8. 牛客网java基础知识

    1.java把表示范围大的数转换为表示范围小的数,需要强制类型转换. Java中,数据类型分为基本数据类型(或叫做原生类.内置类型)和引用数据类型.原生类型为基本数据类型int和布尔值可以相互转换吗? ...

  9. unix简史及应用

    Unix 简史 1965年时,贝尔实验室(Bell Labs)加入一项由奇异电子(General Electric)和麻省理工学院(MIT)合作的计画:该计画要建立一套多使用者.多任务.多层次(mul ...

  10. Latex中为作者添加多个单位属性(IEEE模板)

    \author{ \IEEEauthorblockN{name1 name1\IEEEauthorrefmark{1}\IEEEauthorrefmark{2},  name2 name2\IEEEa ...