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 ...
随机推荐
- Ubuntu(64位)编译Android源码常见错误解决办法
2013年07月10日 14:47:27 阅读数:1239 错误: /usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file ...
- 准确率99%!基于深度学习的二进制恶意样本检测——瀚思APT 沙箱恶意文件检测使用的是CNN,LSTM TODO
所以我们的流程如图所示.将正负样本按 1:1 的比例转换为图像.将 ImageNet 中训练好的图像分类模型作为迁移学习的输入.在 GPU 集群中进行训练.我们同时训练了标准模型和压缩模型,对应不同的 ...
- <video> controlsList
Audio/Video Updates in Chrome 58 <video controls controlsList="nofullscreen nodownload norem ...
- 尝试实现bootstrap3网格系统
这是一篇搁置了很久的博文,个人实现的关键代码如下: // 这是用sass实现的,只是大致实现了网格系统和offset的功能 $size_list: ( xs: 0, sm: 576, md: 992, ...
- 洛谷 p2618 数字工程 记忆化搜索_ 线性筛
我们在线筛的同时处理出每个数的所有质因子,记忆化搜索的时候直接枚举质因子即可. 时间复杂度为 O(nlogn)O(nlogn)O(nlogn) Code: #include<cstdio> ...
- baidu练习/html/css
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Java之秒杀活动解决方案
0 引言 本文主要描述,服务端做相关秒杀活动的时候,对应的解决方案,即高并发下的数据安全. 1 优化方案 1.1 乐观锁思路 Redis中的watch,请求时,通过Redis查询当前抢购数据,如果当前 ...
- oracle中单引号的处理
当想让输出的结果中字段带有单引号', 场景一:连续三个单引号''' select '''helin''' from dual; ---'helin' 场景二:拼接字段的结果集--连续4个单引号 sel ...
- BootStrap复习总结
Bootstrap3 复习总结: 1. 栅格系统: sm:768px md:992px lg:1200px (响应式布局) 1.横向分割页面的空间. 2.在分割的同时考虑到响应式. 内部实现是12列的 ...
- You-Dont-Need-JQuery (你不需要JQuery)
看完这篇文章我才觉得真的要用JQuery ,因为实在是有些地方设计的使用太复杂了, document.querySelector() 和 Document.querySelectorAll 的确是很方 ...