063 Unique Paths II 不同路径 II
这是“不同路径” 的进阶问题:
现在考虑网格中有障碍物。那样将会有多少条不同的路径从左上角到右下角?
网格中的障碍物和空位置分别用 1 和 0 来表示。
例如,
如下所示在 3x3 的网格中有一个障碍物。
[
[0,0,0],
[0,1,0],
[0,0,0]
]
一共有 2 条不同的路径从左上角到右下角。
注意: m 和 n 的值均不超过 100。
详见:https://leetcode.com/problems/unique-paths-ii/description/
Java实现:
参考:https://www.cnblogs.com/springfor/p/3886644.html
class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length; if(m==0||n == 0){
return 0;
} if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1){
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];
}
}
参考:https://www.cnblogs.com/grandyang/p/4353680.html
063 Unique Paths II 不同路径 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). ...
- 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】063. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [LeetCode] Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 62. Unique Paths 不同的路径
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(唯一的路径数)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The ...
- LeetCode OJ:Unique Paths(唯一路径)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode(63):不同路径 II
Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
随机推荐
- openfire插件(1)
插件核心类,这里对PacketInterceptor.Plugin进行继承.如果开发插件就一定要继承Plugin,而继承PacketInterceptor是拦截用户发送的消息包.对消息包进行过滤.拦截 ...
- Collaborative Index Embedding for Image Retrieval
最近看了一篇比较好的文章,效果很好,简单记录一下. 这篇文章的核心思想是,融合两种不同类型的特征.文章中用的是SIFT和CNN提取的特征.还是神经大法好啊. 第一步就是建立两种不同特征的索引,文章用的 ...
- 浅析linux 下shell命令执行和守护进程
执行shell脚本有以下几种方式 1.相对路径方式,需先cd到脚本路径下 [root@banking tmp]# cd /tmp [root@banking tmp]# ./ceshi.sh 脚本执行 ...
- poj1195 Mobile phones
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 19786 Accepted: 9133 De ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Merge into使用详解( 同时执行inserts和updates操作 )
Merge是一个非常有用的功能,类似于MySQL里的insert into on duplicate key. Oracle在9i引入了merge命令, 通过这个merge你能够在一个SQL语句中对一 ...
- Elasticsearch搜索引擎版本配置
简要描述: 搜索引擎版本配置 产品 版本号 ES版本要求 说明 PHP =5.5.38 Java =1.8.0_73 用于支持ES Elasticsearch =2.3.5 搜索引擎 ...
- Linux命令总结_命令执行顺序
有时候,我们需要一个命令执行完之后再去执行另一个命令,使用 &&和 ||可以完成 这样的功能,相应的命令可以是系统命令或shell脚本 Shell还提供了在当前shell或子shell ...
- JVM StackOverflowError vs. OutOfMemoryError
if the computation in a thread needs a larger Java Virtual Machine stack than is permitted, the Java ...
- 《精通Spring4.X企业应用开发实战》读后感第五章(方法注入)