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. uoj 48 核聚变反应强度 次小公因数

    [UR #3]核聚变反应强度 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/48 Description 著名核 ...

  2. CodeM资格赛2

    题目描述 组委会正在为美团点评CodeM大赛的决赛设计新赛制. 比赛有 n 个人参加(其中 n 为2的幂),每个参赛者根据资格赛和预赛.复赛的成绩,会有不同的积分.比赛采取锦标赛赛制,分轮次进行,设某 ...

  3. POJ 3237 Tree (树链剖分)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 2825   Accepted: 769 Description ...

  4. jdk1.8 foreach

    lambda 表达式效率非常低,测试代码可以看到大概3~5倍的差距 遍历Map的方式有很多,通常场景下我们需要的是遍历Map中的Key和Value,那么推荐使用的: public static voi ...

  5. windows10许可证即将过期怎么办

    来自:http://www.xuexila.com/diannao/xitong/win7/1316897.html 会提示需要激活windows,不需要每隔两小时会重启一次,有些应用是运行不起来的. ...

  6. There is no Action mapped for namespace [/] and action name [Login] associated with context path [/e

    近期学习web开发时,就遇到这个令人头疼的问题. 百度谷歌了N遍,最终在博客http://blog.csdn.net/liu578182160/article/details/17266879中找到了 ...

  7. Redis事务为什么不支持回滚

    在事务运行期间,虽然Redis命令可能会执行失败,但是Redis仍然会执行事务中余下的其他命令,而不会执行回滚操作,你可能会觉得这种行为很奇怪.然而,这种行为也有其合理之处:只有当被调用的Redis命 ...

  8. C++ Primer 学习笔记_91_用于大型程序的工具 --命名空间

    用于大型程序的工具 --命名空间 引言: 在一个给定作用域中定义的每一个名字在该作用域中必须是唯一的,对庞大.复杂的应用程序而言,这个要求可能难以满足.这样的应用程序的全局作用域中一般有很多名字定义. ...

  9. 完全理解Gson(2):Gson序列化

    通过调用 Gson API 可以把 Java 对象转换为 JSON 格式的字符串(项目主页).在这篇文章中,我们将会讲到如何通过 Gson 默认实现和自定义实现方式,将 Java  对象转换为 JSO ...

  10. VisualStudio使用GIT

    使用GIT进行源码管理 -- 在VisualStudio中使用GIT GIT作为源码管理的方式现在是越来越流行了,在VisualStudio 2012中,就通过插件的现实对GIT进行了官方支持,并且这 ...