题目:

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的更多相关文章

  1. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  2. 【Leetcode】【Medium】Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  4. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  5. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  6. 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 ...

  7. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  8. 【LeetCode练习题】Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  9. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  10. [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 ...

随机推荐

  1. POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29902   Accepted: 10697 ...

  2. BTrace追踪Hadoop部署

    Hadoop集成BTrace 1.BTrace 1.1BTrace部署 1.下载BTrace工具包,官网地址:https://kenai.com/projects/btrace/downloads/d ...

  3. Template Method Design Pattern in Java

    Template Method is a behavioral design pattern and it’s used to create a method stub and deferring s ...

  4. MSP430 G2553 基本时钟模块+ (Basic Clock Module+)

    一.时钟源 MSP430的Basic Clock Module+支持的时钟源有: DCOCLK:内部数字控制振荡器,Internal digitally contrlled oscillator.所有 ...

  5. 使用centos官方镜像制作jdk8环境镜像

    首先将jdk文件或者tar包放在/var/local路径下 然后Dockerfile中写 # 以 centos7 为基础镜像 FROM centos:latest MAINTAINER chen # ...

  6. LeeDUT个人WEB作品

    *****目前大三前端狗一只,听说博客里写点记点能求OFFER***** 1.微云盘upan.oureda.cn  2013.10 微云盘是基于分布式系统.提供文件分享的校园存储站点,上传文件之后随即 ...

  7. C#实现像微信PC版一样的扫码登录功能

    现在好些网站都支持扫码登录,感觉上安全了很多,但是本地程序扫码登录的不多,就用C#实现了一下,需要作如下准备 在官网上申请一个企业微信,有条件的话做个企业认证吧,我们的是认证过的,所以账号和本地其他系 ...

  8. PARTITION BY函数

    1.PARTITION BY 开窗函数, 使用场景,在合同表里,获取所有房源在最新的合同编号.或者获取每个班级每次考试的第一名. 区别聚合函数:对于每个每个分组返回多行,而聚合函数对于每个分组只返回一 ...

  9. Web开发利器Webstorm导入多个文件夹或者项目

    步骤:File->Setting 打开设置面板,打开Directories节点,然后看到有Add Content Root 操作选项,单击弹出磁盘目录文件,选择对应项目或者目录即可.

  10. Remoteland HDU - 4196

    题意: 给出一个n,在[1, n] 中挑选几个不同的数相乘,求能的到的最大完全平方数 解析: 最大的肯定是n!, 然后n!不一定是完全平方数 (我们知道一个完全平方数,质因子分解后,所有质因子的质数均 ...