LeetCode OJ 63. 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.
【题目分析】
相比较上一个题目,在这个问题的grid中有部分区域是存在障碍物的,即有些区域不能到达。
【思路】
我们分析一下如果存在障碍物这个问题我们该如何解决。
1. 如果障碍物在入口或者出口,那么总的方法数就是0;
2. 如果障碍物在其他位置,那么存在障碍物的位置能到达的方法数为零;
在上一题中用到的方法在这个题目中也适用,只是需要判定一下当前是否是障碍物,如果是障碍物,则直接把能到达当前位置的方法数设置为0,否则:A[j] = A[j] + A[j - 1];
如上图中,黑框表示该位置存在障碍物,那么能到达这个位置的方法数只能被设置为0,然后按照动态规划的方法一行一行遍历,求出能到达每一个位置的方法数,直到求出能到达出口的方法数为止。
【java代码】
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int row = obstacleGrid.length;
int col = obstacleGrid[0].length; if(row == 0 || col == 0) return 0; int[] dp = new int[col];
dp[0] = 1; for (int i = 0; i < row; i++){
if(obstacleGrid[i][0] == 1) dp[0] = 0;
for (int j = 1; j < col; j++){
if(obstacleGrid[i][j] == 1) dp[j] = 0;
else dp[j] = dp[j - 1] + dp[j];
}
} return dp[col - 1];
}
}
LeetCode OJ 63. Unique Paths II的更多相关文章
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- [leetcode DP]63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode OJ:Unique Paths II(唯一路径II)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeedCode OJ]#63 Unique Paths II
[ 声明:版权全部,转载请标明出处,请勿用于商业用途. 联系信箱:libin493073668@sina.com] 题目链接:https://leetcode.com/problems/uniqu ...
- leetcode@ [62/63] Unique Paths II
class Solution { public: int uniquePathsWithObstacles(vector<vector<int>>& obstacleG ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
随机推荐
- FileOutputStream flush()
FileOutputStream 继承 OutputStream ,flush方法查看源码方法体为空,所以flush没起到清除缓存的作用 改用BufferedOutputStream再调用flush( ...
- openSuse快捷键
1.printscreen全屏截图 2.ctrl+printscreen窗口截图 3.shift+printscreen选择截图 4.Ctrl+Alt+up arrow.Ctrl+Alt+down a ...
- XTU 1246 Heartstone
$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$D$题 贪心. 我是这样贪的:开三个优先队列$q[0]$,$q[1]$,$q[2]$,$q[i]$存储对$3$取余之后为$i$的数. 首先看看还 ...
- CentOs + Nginx + php-fpm + MySql 依赖库安装
依赖库和开发工具 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype free ...
- HTTPS详解
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer,基于SSL的HTTP协议)使用了HTTP协议,但HTTPS使用不同于HTTP协议的 ...
- 最受欢迎的iOS第三方SDK
http://www.raywenderlich.com/forums/viewtopic.php?t=4496
- Oracle数据库创建数据库实例1
http://jingyan.baidu.com/article/ae97a646d128d5bbfd461d00.html
- FileZilla客户端源码解析
FileZilla客户端源码解析 FTP是TCP/IP协议组的协议,有指令通路和数据通路两条通道.一般来说,FTP标准命令TCP端口号是21,Port方式数据传输端口是20. FileZilla作为p ...
- windows线程池四种情形(win核心读书笔记)
windows线程池四种情形(win核心读书笔记) Mircosoft从Windows2000引入线程池API,并在Vista后对线程池重新构架,引入新的线程池API.以下所有线程池函数均适用于Vis ...
- mac下配置gdb调试golang
mac下配置gdb调试golang 原文链接 https://sourceware.org/gdb/wiki/BuildingOnDarwin Building GDB for Darwin Crea ...