Unique path 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.
题目分析:
是机器人从左上角走到右下角的路径统计数(仅仅能向右或者向下走),这跟第一题不同的是设置了路障。不能用第一题的Cm-1m+n-2(參考http://blog.csdn.net/sinat_24520925/article/details/45769317)种来计算。
以下我们用第二种思路来求该类题,假设机器人要到达位置(i,j),则它到(i,j)是从(i-1,j)向下一步到达,或是从(i,j-1)向右一步到达,也就是说到达(i,j)的路径数是到达(i-1,j)路径数+到达(i,j-1)路径数.如图要到达星星必须经过星星上面方框或者左边方框。即r[i,j]=r[i-1,j]+r[i,j-1]
由于我们按行依次往下找。所以第一行遍历完之后得到第一行全部路径res(n),第二行的时候res(n)值没变。指的是上一行的结果,我们最后仅仅需知道最后一行的结果就可以,所以用第二行的res将第一行的替换掉。此时r[i-1,j]指的是res[j],而r[i,j-1]指的是res[j-1],所以更新得到的res[j]=res[j]+res[j+1].
这样的解题思路为动态规划。这样能够简单求解path sum的第一题,在第二题时,稍作变动就可以。放置障碍物的那一个res[j]=0就可以。
第一题无障碍的代码例如以下:
class Solution {
public:
int uniquePaths(int m, int n) {
if(m==0||n==0) return 0;
vector<int> res(n,0);
res[0]=1;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
res[j]+=res[j-1];
}
return res[n-1];
}
};
第二题有障碍物的代码例如以下:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if(obstacleGrid.size() == 0 || obstacleGrid[0].size()==0)
return 0;
int m=obstacleGrid.size();
int n=obstacleGrid[0].size();
vector<int> res(n,0);
res[0]=1;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(obstacleGrid[i][j]==1)
res[j]=0;
else
{
if(j!=0)
res[j]+=res[j-1];
}
}
}
return res[n-1];
}
};
Unique path ii的更多相关文章
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode63—Unique Path II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
随机推荐
- python爬虫系列序
关于爬虫的了解,始于看到这篇分析从数据角度解析福州美食,和上份工作中的短暂参与. 长长短短持续近一年的时间,对其态度越来越明晰,噢原来这就是我想从事的工作. 于是想要系统学习的心理便弥散开来…… 参考 ...
- 移植MonkeyRunner的图片对照和获取子图功能的实现-Appium篇
假设你的目标測试app有非常多imageview组成的话,这个时候monkeyrunner的截图比較功能就体现出来了. 而其它几个流行的框架如Robotium,UIAutomator以及Appium都 ...
- javaScript实现日历控件
近期学习js.闲来无事就写了个简单的日历控件.不知道别人是怎么实现的.纯粹自己想法写的, 写的不好 ,但能够起到抛砖引玉的作用. 先来看效果. watermark/2/text/aHR0cDovL2J ...
- Bash脚本中的操作符
一.文件測试操作符 假设以下的条件成立将会返回真. -e 文件存在 -a 文件存在 这个选项的效果与-e同样. 可是它已经被"弃用"了, 而且不鼓舞使用. -f 表示这个文件是一个 ...
- 朴素的UNIX之-调度器细节
0.多进程调度的本质 我们都知道UNIX上有一个著名的nice调用.何谓nice,当然是"好"了.常规的想法是nice值越大越好,实际上,nice值越好,自己的优先级越低.那么为何 ...
- MYSQL Training: MySQL I
让以admin身份登录.源代码: 非常easy的注入 在username输入 admin' OR '1'='1 OK.
- 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点
题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...
- DateTime? 定义的日期变量如何format
DateTime? dateTime; dateTime.Value.ToString("yy-MM-dd")
- Route学习笔记
前言 UrlRoutingModule.class:这块的代码关联了上一篇中路由部分的一个详细说明 一:Route的讲解 1. 路由模板匹配 添加路由: MapRoute 剔除的路由:IgnoreRo ...
- CommandType.Text
CommandType.Text代表执行的是SQL语句CommandType.StoreProcedure代表执行的是存储过程CommandType代表要执行的类型 //返回DataTable的SQL ...