【LeetCode练习题】Unique Paths II
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
and0
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.
在Unique Paths 的基础上增加了障碍物。问从起点到终点有多少种走法。
解题思路:
基于上一篇博客,即Unique Paths这道题里的最后一个方法,就是空间复杂度O(min(m,n))的那个解法,接下来的算法的原理就是基于那个的。
因为起点到右边的点路径是唯一的,到下边点路径是唯一的。
所以可以得到一条重要的规律:
起点到终点的路径数等于右边那个点到终点路径数与下边那个点到终点路径数的和。
大概过程是酱紫的:
- 用一个长度为n+1的vector对每一列dp 。
- 初始化一个长度为n+1,所有值都为0的vector<int> dp
- 最开始的时候初始化最后一行,如果不是障碍物,就将dp的对应位置变成1
- 然后分别对倒数第二行,倒数第三行……第一行更新dp[i],dp[i] = (mat[m][i] == 1) ? 0 : dp[i] + dp[i+1];
- 最后dp中存储的是第一行的每一个非障碍物点到终点的路径数。
- dp[0]就是我们的起点啦!~
代码如下:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
vector<int> dp(n+,);
m--;
int i = n-;
while(i >= && obstacleGrid[m][i] != ){
dp[i] = ;
--i;
}
while(m-- > ){ //这种循环判断可以将只有一行的情况统一考虑进去
for(i = n-; i >= ; i--){
dp[i] = (obstacleGrid[m][i] == )? : dp[i+] + dp[i];
}
}
return dp[];
}
};
【LeetCode练习题】Unique Paths II的更多相关文章
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【矩阵】【回溯】【Leetcode】Unique Paths II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
随机推荐
- Linux Java的环境变量搭建
JAVA JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载完后,解压完并将其中的jdk文件夹移动到/u ...
- Animation Override Controller动画重载器
假设游戏有很多个小人, 每一个人有2种动画站立,跑. 在通常情况下每一个人物都需要一个动画控制器. 有没有想过定义一个动画控制器 无须在定义全新的动画充值器实现每一个小人都播放自己的动画呢?没错An ...
- iOS程序员对算法的要求
算法和数据结构(鉴于二者的关联,以下统称算法),对于程序员的重要性一直是个具有争议性的话题.有一些程序员内心对算法有着天然的排斥,面试当中一旦考察算法知识,会被不少程序员吐槽,但有部分公司又一直在坚持 ...
- Direct3D 11的Device接口和DeviceContext接口
D3D的两个主要的接口: Device,ID3D11Device.创建资源,Shader对象,状态对象,查询对象,等.以及检查硬件功能,调试函数.可以认为是资源的提供者. Device Context ...
- [Immutable.js] Lightning Fast Immutable.js Equality Checks with Hash Codes
While Immutable.js offers .is() to confirm value equality between iterables it comes at the cost of ...
- Memcached完全解剖–1. memcached基金会
翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西非常实用,希望大家喜欢. 发表日:2008/7/2 作者:长野雅广(Masahiro Nagano) 原文链接:ht ...
- C#中的一种按日期分文件夹的日志写法
众所周知,日志是调试程序的有效途径,有一个好的日志代码,是一个程序小猿梦寐以求的. 以下是我结合网上资源自己总结的一小段代码,请笑纳: 转载请注明来源: http://www.cnblogs.com/ ...
- Code Complete 读后总结和新的扩展阅读计划
Code Complete 读后总结和新的扩展阅读计划 用了一年时间终于将代码大全读完了,在这里做一个简单的总结,并安排下一阶段的扩展阅读计划. 1.选择代码大全作为我程序员职业入门的第一本书,我认为 ...
- NSString / NSMutableString 字符串处理,常用代码 (实例)
http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...
- Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. SwapSort time limit per test 1 seco ...