[LeetCode][Java] 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.
题意:
紧跟着题目《Unique Paths》,现给出这样一题目:
假设在格子中加入一些障碍,会出现多少存在且唯一的不同路径呢?
障碍和空白格子分别被标记为1
and 0
.
比方一个3x3的格子中的中间存在一个障碍,例如以下所看到的:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
总的路径数为2.
算法分析:
思路与题目《Unique Paths》类似,不同之处为:
初始化边界上行和列时,出现障碍。后面路径数dp的都是0
中间的格子出现障碍时,该格子dp表示的路径数直接填0
AC代码:
public class Solution
{
public int uniquePathsWithObstacles(int[][] obstacleGrid)
{
if(obstacleGrid==null||obstacleGrid.length==0)
return 0;
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int [][] dp = new int[m][n];
for(int i = 0; i < m; i++)
{
if(obstacleGrid[i][0]!=1)
dp[i][0] = 1;
else
break;
}
for(int j = 0; j < n; j++)
{
if(obstacleGrid[0][j]!=1)
dp[0][j] = 1;
else
break;
}
for(int i = 1; i < m; i++)
{
for(int j = 1; j< n; j++)
{
if(obstacleGrid[i][j]!=1)
dp[i][j] = dp[i-1][j] + dp[i][j-1];
else
dp[i][j]=0;
}
}
return dp[m-1][n-1];
}
}
[LeetCode][Java] Unique Paths II的更多相关文章
- 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 Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- Java for LeetCode 063 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 不同的路径之二
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
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【矩阵】【回溯】【Leetcode】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 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
随机推荐
- HTML5之中国象棋,附带源码!
好久没写随笔了,好怀恋2013年的日子,因为现在不能回到过去了! 再见了 感谢你为我做的一切! 进入正题:HTML5之中国象棋 很小就会下象棋了, 这是象棋的测试地址:点击我吧 然后点击里面的象 ...
- python 学习分享-文件操作篇
文件操作 f_open=open('*.txt','r')#以只读的方式(r)打开*.txt文件(需要与py文件在同一目录下,如果不同目录,需写全路径) f_open.close()#关闭文件 打开文 ...
- day03_12 缩进介绍
python比较变态,必须缩进,而C可以不缩进,世界上只有python这门语言要求必须缩进 tab键是缩进,shift+tab往左移动 SyntaxError:invalid syntax 语法错误 ...
- cobbler dell r730安装问题(四)
环境介绍: 服务器硬件:dell-13代 R730 Intel xeon E5-2600系列CPU:E5-2609 v4.E5-2620 v4.E5-2650 v4 cobbler版本:cobbler ...
- Leetcode 472.连接词
连接词 给定一个不含重复单词的列表,编写一个程序,返回给定单词列表中所有的连接词. 连接词的定义为:一个字符串完全是由至少两个给定数组中的单词组成的. 示例: 输入: ["cat" ...
- andorid studio 环境搭建
1 安装jdk,配置jdk的环境变量http://www.cnblogs.com/liuhongfeng/p/4177568.html(通过java ,javac, java -version来察看j ...
- 错误处理: Python值传递和引用传递的问题
1.插入数据库的时候报错110, 提示columns数量少于插入的值内容. 2.核对了下栏目并没有少,打印出插入的值,看看值是不是多了. 查看了下,确实第二次值的时候长度边长了,第二次把第一次的部分值 ...
- PHP下mysql驱动概述
Overview of the MySQL PHP drivers 什么是API? 一 个应用程序接口(Application Programming Interface的缩写),定义了类,方法,函数 ...
- RobotFramwork自定义库
这么长时间才知道有RobotFramwork这东西... 感叹之前都干啥去了,感叹公司为啥不用这货? 网上的安装文档都有,就不用自己在记录啦. 感觉在实际实用时,肯定要有自己定义的库啊,不能只用bui ...
- P2622 关灯问题II (状态压缩,最短路)
题目链接 Solution 这道题算是很经典的状压问题了,好题. 考虑到 \(n\) 的范围仅为 \(10\) , 那么也就是说所有状态压起来也只有 \(1024\) 种情况. 然后我们发现 \(m\ ...