LeetCode 63. 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.
题目标签:Array
题目给了我们一个matrix,里面1代表有障碍物,0代表空。这道题目和Unique Path 基本一样。之前我们是给start point 1的值,然后遍历matrix,拿left 和 top的值加起来。那么这题,1被用了,代表障碍物。那我们就用-1。首先来分析一下,如果起点或者终点都是1的话,那么就无路可走,直接return 0就可以。剩下的情况,对于每一个点,如果它是障碍物,就直接跳过;如果不是,那么拿left 和top 的加一起,那如果left 和 top 有障碍物的话,也跳过。最后把终点的值 * -1 就可以了。
当然,也可以另外新建一个matrix,过程是同样的方法,不同的是当这个点是1的话,就把这个点的值等于0。 这方法就需要多建一个matrix。
Java Solution:
Runtime beats 14.83%
完成日期:07/21/2017
关键词:Array
关键点:Dynamic Programming, 逆向思考
public class Solution
{
public int uniquePathsWithObstacles(int[][] obstacleGrid)
{
if(obstacleGrid[0][0] == 1 ||
obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1] == 1) // obstacle at start point or finish point
return 0; obstacleGrid[0][0] = -1; // start point for(int i=0; i<obstacleGrid.length; i++) // row
{
for(int j=0; j<obstacleGrid[0].length; j++) // column
{
// if this is not obstacle
if(obstacleGrid[i][j] !=1)
{
// get left: left is not obstacle
if(j-1 >=0 && obstacleGrid[i][j-1] !=1)
obstacleGrid[i][j] += obstacleGrid[i][j-1];
// get top: top is not obstacle
if(i-1 >=0 && obstacleGrid[i-1][j] !=1)
obstacleGrid[i][j] += obstacleGrid[i-1][j];
} }
} return obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1] * -1;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 63. Unique Path II(所有不同路径之二)的更多相关文章
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [leetcode] 63. Unique Paths II (medium)
原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 63. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
随机推荐
- java :equals()和hashcode()方法的结合使用
哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: 1 public native int hashCode(); 根据 ...
- SSH第一篇【整合SSH步骤、OpenSessionInView】
前言 到目前为止,Struts2.Hibernate.Spring框架都过了一遍了.也写过了Spring怎么与Struts2整合,Spring与Hibernate整合-本博文主要讲解SSH的整合 整合 ...
- 一条语句导致CPU持续100%
一大早收到一堆CPU预警邮件,通常每天只在统计作业执行期间会收到2~3封CPU预警邮件.这次的预警来自另一台服务器,并且明细数据显示其CPU一直维持在49%.登录到服务器,查看任务管理器(查看资源监视 ...
- jmeter 压测最近的心得体会
笔者14年入坑测试,截止到17年年初一直在游戏公司,压测,我都没有怎么用过,特别是jmeter去压测,自己学习,可是先找到切入点,于是乎, 其实也算是我学习后,先找一个更大的平台吧,我聊了几个游戏公司 ...
- [js高手之路] es6系列教程 - promise常见用法详解(resolve,reject,catch,then,all,race)
关于promise我在之前的文章已经应用过好几次,如[js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist,本文就来讲解下pro ...
- composer设置忽略版本匹配
composer install 时遇到错误: Your requirements could not be resolved to an installable set of packages. 原 ...
- 零基础的人该怎么学习JAVA
对于JAVA有所兴趣但又是零基础的人,该如何学习JAVA呢?对于想要学习开发技术的学子来说找到一个合适自己的培训机构是非常难的事情,在选择的过程中总是 因为这样或那样的问题让你犹豫不决,阻碍你前进的 ...
- 第4章 同步控制 Synchronization ---哲学家进餐问题(The Dining Philosophers)
哲学家进餐问题是这样子的:好几位哲学家围绕着餐桌坐,每一位哲学家要么思考,要么等待,要么就吃饭.为了吃饭,哲学家必须拿起两支筷子(分放于左右两端).不幸的是,筷子的数量和哲学家相等,所以每支筷子必须由 ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
- Redis “瘦身”指南
code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...