Unique Paths II leetcode java
题目:
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 Path是一样的。
只是要单独考虑下障碍物对整个棋盘的影响。
先看看初始条件会不会受到障碍物的影响。
假设整个棋盘只有一行,那么在第i个位置上设置一个障碍物后,说明位置i到最后一个格子这些路都没法走了。
如果整个棋盘只有一列,那么第i位置上的障碍物,也会影响从第i位置往后的路。
所以说明,在初始条件时,如果一旦遇到障碍物,障碍物后面所有格子的走法都是0。
再看求解过程,当然按照上一题的分析dp[i][j] = dp[i-1][j] + dp[i][j-1] 的递推式依然成立(机器人只能向下或者向右走嘛)。但是,一旦碰到了障碍物,那么这时的到这里的走法应该设为0,因为机器人只能向下走或者向右走,所以到这个点就无法通过。
处理完障碍物的特殊问题,依照unique paths改一下代码就好。
代码如下:
- 1 public int uniquePathsWithObstacles(int[][] obstacleGrid) {
- 2 int m = obstacleGrid.length;
- 3 int n = obstacleGrid[0].length;
- 4
- 5 if(m==0||n == 0)
- 6 return 0;
- 7
- 8 if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1)
- 9 return 0;
- int[][] dp = new int[m][n];
- dp[0][0] = 1;
- for(int i = 1; i < n; i++){
- if(obstacleGrid[0][i] == 1)
- dp[0][i] = 0;
- else
- dp[0][i] = dp[0][i-1];
- }
- for(int i = 1; i < m; i++){
- if(obstacleGrid[i][0] == 1)
- dp[i][0] = 0;
- else
- dp[i][0] = dp[i-1][0];
- }
- for(int i = 1; i < m; i++){
- for(int j = 1; j < n; j++){
- if(obstacleGrid[i][j] == 1)
- dp[i][j] = 0;
- else
- dp[i][j] = dp[i][j-1] + dp[i-1][j];
- }
- }
- return dp[m-1][n-1];
- }
Unique Paths II leetcode java的更多相关文章
- Unique Paths II [LeetCode]
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique Paths II ——LeetCode
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [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 Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- Codechef September Challenge 2018 游记
Codechef September Challenge 2018 游记 Magician versus Chef 题目大意: 有一排\(n(n\le10^5)\)个格子,一开始硬币在第\(x\)个格 ...
- 《Go语言实战》Go 类型:基本类型、引用类型、结构类型、自定义类型
Go 语言是一种静态类型的编程语言,所以在编译器进行编译的时候,就要知道每个值的类型,这样编译器就知道要为这个值分配多少内存,并且知道这段分配的内存表示什么. 提前知道值的类型的好处有很多,比如编译器 ...
- XAML / Self binding, bindingcontext
Hello, I want to bind the text property of a editor element to BindableProperty. Here is what I trie ...
- .Net Discovery系列之十一-深入理解平台机制与性能影响 (中)
上一篇文章中Aicken为大家介绍了.Net平台的垃圾回收机制与其对性能的影响,这一篇中将继续为大家介绍.Net平台的另一批黑马—JIT. 有关JIT的机制分析 ● 机制分析 以C#为例, ...
- Unity3D实践系列06,球体撞击物体游戏
本篇实现一个球体在固定区域移动撞击Cube的游戏. 首先有1个Plane当作地面,1个Sphere当作球体,4个Cube当作墙,12个Cube当作被撞击物体,另外还有球体的撞击计算,在撞击的过程适时显 ...
- How To Create A Local Repository For SUSE Linux
原文地址:http://candon123.blog.51cto.com/704299/1009294/ As you know,you can use the yum command to inst ...
- Informix存储过程
一.存储过程概述 存储过程是一个用户定义的函数,由存储过程语句(SPL) 和一组SQL语句组成,以可以执行代码形式存储在数据库中,和表.视图.索引等一样,是数据库的一种对象. 存储过程语言SPL(St ...
- <fmt:formatNumber>标签
<fmt:formatNumber>标签用于格式化数字,百分比,货币. 属性 <fmt:formatNumber>标签有如下属性: 属性 描述 是否必要 默认值 value 要 ...
- 傲骨贤妻第一季/全集The Good Wife迅雷下载
第一季 The Good Wife Season 1 (2009)看点:在经受丈夫Peter的背叛以及因此而带来的公众羞辱后,Alicia Florrick选择重新继续自己原来的事业,一名辩护律师,以 ...
- SRE学习笔记:分布式共识系统、Paxos协议
最近阅读了<SRE Google运维解密>的第23章,有一些感触,记录一下. 日常工作中,我们经常需要一些服务分布式的运行.跨区域如跨城.跨洲部署运行分布式系统往往是容易的,但是如何保证各 ...