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 ...
随机推荐
- 在代理中托管特殊方法的python代码实现
任务简单的介绍是: 在新风格对象模型中,Python操作其实是在类中查找特殊方法的(经典对象是在实例中进行操作的),现在需要将一些新风格的实例包装到代理中,,此代理可以选择将一些特殊的方法委托给内部的 ...
- WebForm 内置对象QueryString、Repeater删改
一.内置对象QueryString--地址栏数据拼接 格式:?key=value 如:string path = "Default2.aspx?aaa=" + TextBox1.T ...
- 使用FormData,进行Ajax请求并上传文件
前段时间做了个手机端的图片上传,为了用户体验,用ajax交互,发现了FromData对象,这里有详细解释https://developer.mozilla.org/zh-CN/docs/Web/API ...
- php强制下载文件并显示原始文件名
原来一直没有接触过,这几天一直在玩儿文件上传下载的东西.今天又遇到一个坑. 描述:文件上传至服务器后,如果是rar或则其他的非浏览器直接识别的格式,用户点击链接了后是可以直接就被下载下来的.那么如果上 ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- UserDefault数据读取
//GameScene.h #include "cocos2d.h" USING_NS_CC; class GameScene : public cocos2d::Layer{pu ...
- 【for陷阱】遍历的同时删除元素
今晚,哦不,是昨晚了,想删除空行时,给for语句和列表坑得好惨!!! 一般来说,删除字符串的空行有以下几种常见的方法~(然而我竟然想不出来) 假设我们要把下面的字符串之间的空行给去掉 # coding ...
- Uploadify 上传后的文件删除,上传队列无法更新问题
1. 定义一个上传限制数量 var uploadLimit = 3; 2. 点击页面的删除图片成功后,将uploadLimit++操作 3. 通过uploadify的settings方式重置上传限制数 ...
- Python subprocess + timeout的命令执行
Popen对象 poll() 判断是否执行完毕,执行完毕返回0,未执行完毕返回None terminate() 终止进程发送SIGTERM信号 raise 自定义返回错误 import time im ...
- stm32 串口乱码的解决
版权声明:本文为博主原创文章. 前几天在中移物联网申请了一个迷你开发板,运行官方提供的程序,感觉板子是正常的.但是自己写的程序能够刷到板子上,但是串口却是乱码.官方和我的额程序都是用的库函数的方式写的 ...