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 ...
随机推荐
- maven profile切换正式环境和测试环境
有时候,我们在开发和部署的时候,有很多配置文件数据是不一样的,比如连接mysql,连接redis,一些properties文件等等 每次部署或者开发都要改配置文件太麻烦了,这个时候,就需要用到mave ...
- python之异常
一.异常与错误 1.语法错误 错误一: if 错误二: def text: pass 错误三: print(sjds 2.逻辑错误 #用户输入不完整(比如输入为空)或者输入非法(输入不是数字) num ...
- Struts2第五篇【类型转换器、全局、局部类型转换器】
前言 上篇博文已经讲解了,Struts2为我们实现了数据自动封装-由上篇的例子我们可以看出,表单提交过去的数据全都是String类型的,但是经过Struts自动封装,就改成是JavaBean对应成员变 ...
- JDBC操作数据库之删除数据
删除数据使用的SQL语句为delete语句,如果删除图书id为1的图书信息,其SQL语句为: delete from book where id=1 在实际开发中删除数据通常使用PreparedSta ...
- 如何用一天时间实现自己的RPC框架
前言 最近,闲来无事,自己写了一个简单的RPC框架,我把它叫做SimpleRpc.它有多简单?一共只有1400行代码.这个RPC只是作为自己试验作品,交流技术之用,当然如果你敢用,也可以放到生产环境之 ...
- struts2前后台交互
1.前台到后台A.form提交,后台用getParameter()方法拿到数据:B.url用?+&C.Ajax使用data:{username:account,password:passwor ...
- 尝试在Linux上部署Asp.net Core应用程序
快两个月没接触.net,倒是天天在用Linux,所以想尝试一下在Linux运行喜欢的.net 应用. 安装CentOS 安装.Net core for Linux 创建Asp.net Core应用程序 ...
- 在MAC OS X中默认的Web共享目录
在Mac OS X中可以很方便的通过开启"Web共享"启用Apache服务:设置方法如下: 打开"系统设置偏好(System Preferences)" -&g ...
- Linux入门之常用命令(12)用户管理
[用户管理] linux如何查看所有的用户和组信息的方法: 1.cat /etc/passwd: 2.cat /etc/group 1. useradd useradd 命令可以创建一个新的用户帐号, ...
- java基本的要点
我想告诉大家的不是什么java基本要点,只是对初学者的一点忠告,本人是从八维学校亲身经历过的学生,要想学好并且快速了解java,那你首先必须有英语底子,没有英语底子,几个单词都不会的,我觉得还是放弃学 ...