【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
1and0respectively 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/
随机推荐
- <转载>模板声明中template <typename T>和template <class T>
原文地址http://blog.csdn.net/bug07250432/article/details/10150625 在c++Template中很多地方都用到了typename与class这两个 ...
- vi常用命令集锦
转载,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6575232 vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握这三种模式十分 ...
- Unity 代码检测单击,双击,拖放
今天小伙伴问我如何自己写一段代码检测 单击 双击 和 拖放.于是就写了这段代码O(∩_∩)O~ 代码如下: using UnityEngine; using System.Collections; p ...
- To Miss Our Children Time(dp)
To Miss Our Children Time Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Jav ...
- 【桌面虚拟化】之三 Persistent vs NonP
作者:范军 (Frank Fan) 新浪微博:@frankfan7 在[桌面虚拟化]之二类型及案例中我们探讨了桌面虚拟化的两种架构,HostedVirtual Desktop (VDI) 和 Publ ...
- [Regular Expressions] Find the Start and End of Whole Words
Regular Expression Word Boundaries allow to perform "whole word only" searches within our ...
- Rhythmbox乱码的解决的方法
近期尝试 Listen 和 Banshee 才发现,Rhythmbox 上出现的 mp3乱码问题依然,并且更加严重,想要彻底弄清和解决必须搞清两点,第一, mp3 标签类型和编码,第二,各种播放器对 ...
- BOOST中如何实现线程安全代码
1 需要include <boost/thread.hpp> 2 背景知识请参考<boost程序库完全开发指南>第12章 3 编绎:g++ -o mthread mthread ...
- git错误:fatal: Not a git repository (or any of the parent directories): .git
git错误:fatal: Not a git repository (or any of the parent directories): .git 我用git add file添加文件时出现这样错误 ...
- Hacker(六)----黑客藏匿之地--系统进程
windows系统中,进程是程序在系统中的依次执行活动.主要包括系统进程和程序进程两种. 凡是用于完成操作系统各种功能的各种进程都统称为系统进程: 而通过启动应用程序所产生的进程则统称为程序进程. 由 ...