【Leetcode】【Medium】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.
解题思路:
请参见Unique Paths I 的思路,对于Unique Paths II 我们依然希望使用o(n)的额外空间和o(m+n)的时间复杂度;
Unique Paths II中grid[i][n-1]和grid[i-1][n-1]不再总是相等,即格子中最右侧一列每一格存在的路径条数可能为1或0;
因此,为了继续使用Unique Paths I中数组迭代的技巧,需要每次迭代前计算新一轮的grid[i][n-1]值,这样才能继续计算grid[i][0]~grid[i][n-2]的值,从而完成此次迭代;
如果原始格子内为1,则对应此位值置0,表示从此位置不存在到终点的有效路径。
代码:
- class Solution {
- public:
- int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
- int m = obstacleGrid.size();
- int n = obstacleGrid[].size();
- vector<int> col (n, );
- col[n-] = obstacleGrid[m-][n-] == ? : ;
- for (int i = m - ; i >= ; --i) {
- col[n-] = obstacleGrid[i][n-] == ? : col[n-];
- for (int j = n - ; j >= ; --j) {
- col[j] = obstacleGrid[i][j] == ? : col[j] + col[j+];
- }
- }
- return col[];
- }
- };
注:
养成好习惯,除非特殊说明,不要在原始入参上修改,尤其是入参是地址形式。
【Leetcode】【Medium】Unique Paths II的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- Linux工具安装配置
1.修改主机名/添加别名访问 修改/etc/sysconfig/network中的hostnameNETWORKING=yesHOSTNAME=dlserver01; 修改/etc/hosts文件 1 ...
- Python数据类型(列表)
文章内容参考了教程:http://www.runoob.com/python/python-basic-syntax.html#commentform Python 列表(List) 序列是Pytho ...
- selenium+Python(表单、多窗口切换)
1.多表单切换 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...
- CSS生成小三角
前言:小三角的应用场景:鼠标移动到某个按钮上面,查看信息详情时,信息详情弹出框有时候会需要一个小三角. 代码如下: <div id='triangle'></div> #tri ...
- C++11并发编程:多线程std::thread
一:概述 C++11引入了thread类,大大降低了多线程使用的复杂度,原先使用多线程只能用系统的API,无法解决跨平台问题,一套代码平台移植,对应多线程代码也必须要修改.现在在C++11中只需使用语 ...
- 【wordpress】wordpress初探
接下来,开始wordpress之旅! 访问wordpress文件夹下的index.php 点击现在就开始. 这里要求我们输入数据库名. 所以先去mysql中新建一个wordpress库 create ...
- Tensorflow中的数据对象Dataset
基础概念 在tensorflow的官方文档是这样介绍Dataset数据对象的: Dataset可以用来表示输入管道元素集合(张量的嵌套结构)和"逻辑计划"对这些元素的转换操作.在D ...
- IT自由职业者的第一个月(下)——为什么放弃5年嵌入式驱动开发转到WEB开发?
如果单从兴趣来看,其实我对Linux内核,Android中间件的兴趣要高于WEB,何况还有这么多年的经验积累,何必从头探索一个新的技术方向呢? 这里面原因是很多的,最核心的大概是以下4 ...
- WeUI logo专为微信设计的 UI 库 WeUI
http://www.oschina.net/p/weui?fromerr=FnwHyWAb http://weui.github.io/weui/
- Fatal error: Call-time pass-by-reference has been removed in *****.php on line 18
问题描述:最近刚刚将php升级到5.4.13,但是打开一个页面的时候出现报错:Fatal error: Call-time pass-by-reference has been removed in ...