【数组】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的区别在于路线中有了障碍物,只需把障碍物处路线数变为0即可。
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function(obstacleGrid) {
var f=[];
var m=obstacleGrid.length,n=obstacleGrid[0].length;
for(var i=0;i<m;i++){
f[i]=[];
} for(var i=0;i<m;i++){
if(obstacleGrid[i][0]==1){
f[i][0]=0;
for(var j=i+1;j<m;j++){
f[j][0]=0;
}
break
}else{
f[i][0]=1;
}
} for(var i=0;i<n;i++){
if(obstacleGrid[0][i]==1){
f[0][i]=0;
for(var j=i+1;j<n;j++){
f[0][j]=0;
}
break;
}else{
f[0][i]=1
}
} for(var i=1;i<m;i++){
for(var j=1;j<n;j++){
if(obstacleGrid[i][j]==1){
f[i][j]=0;
}else{
f[i][j]=f[i-1][j]+f[i][j-1];
}
}
} return f[m-1][n-1]; };
【数组】Unique Paths II的更多相关文章
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【Leetcode】【Medium】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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 —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 61. Unique Paths && Unique Paths II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- [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 ...
随机推荐
- Java动态代理(三)——模拟AOP实现
以下案例模拟AOP实现 目录结构 接口PersonService package com.ljq.service; public interface PersonService { public vo ...
- CGLIB实现动态代理
JDK动态代理和CGLIB字节码生成的区别? * JDK动态代理只能对实现了接口的类生成代理,而不能针对类 * CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法 因为是继承 ...
- [转载]MVC、MVP以及Model2(下)
通过采用MVC模式,我们可以将可视化UI元素的呈现.UI处理逻辑和业务逻辑分别定义在View.Controller和Model中,但是对于三者之间的交互,MVC并没有进行严格的限制.最为典型的就是允许 ...
- git@oschina使用入门(图形界面版)
首先,如果你想使用git@oschina ,你的电脑上必须先有git工具:你可以从这里获取谷歌提供的git.exe http://git-scm.com/当然,如果你能熟练通过命令行操作git,那么这 ...
- pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
# 背景 安装pip后发现执行pip install pytest,提示下面错误 pip is configured with locations that require TLS/SSL, howe ...
- AngularJs2 构建简单的英雄编辑器
参考上一篇文章的步骤,重新把相关环境准备.目录结构走一遍. 这一次我们要开始创建真正的Angularjs项目了. 显示我们的英雄 我们要在应用中显示英雄数据 我们来为 AppComponent 添加两 ...
- MongoDB账号管理及实践
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 目前蜂巢(云计算基础服务)MongoDB上已经有数十个实例,其中不少是企业用户或公司内部产品用户的.用户多了 ...
- Android 音频系统得框架
http://www.mamicode.com/info-detail-1790053.html http://blog.csdn.net/lushengchu_luis/article/detail ...
- H - Graphics(dfs)
H - Graphics Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submi ...
- 【ocp-12c】最新Oracle OCP-071考试题库(42题)
42.(9-1)choose the best answer: Which statement is true about the Oracle SQL, DELETE and TRUNCATE st ...