题目:

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. <网络攻防实践> 课程总结20169216

    课程总结20169216 每周作业链接汇总 第一周作业:Linux基础入门(1-5).基本概念及操作 第二周作业:linux基础入门(6-11).网络攻防技术概述网络攻防试验环境搭构.Kali教学视频 ...

  2. 关于数组以及c#学习问题

    第二次作业我没注意看群通告,看到都3月8号,开始着手想用c#试着写写,才发现一些问题. a.鞠老的要求中必须原数据需要csv文件,csv文件不是太了解,网上简单查阅了一下------csv意思是逗号分 ...

  3. SQLServer中数据加密方法

    对SQLServer中的数据进行加密,有三种方法, 1.  在程序语言中先对数据进行加密后再把加密后的数据保存在SQLServer数据库中: 2.  利用SQLServer未公开的加密密码函数,在SQ ...

  4. Angularjs 实现页面遮罩层功能

    实现效果: 1.loading指令: "use strict" /** * Created by yw on 2015/9/27. * user defined loading d ...

  5. c# 中 $符号的用法

    var names = new List<string> { "jason", "Ana", "Felipe" }; forea ...

  6. SQL Union 和Union All 的区别

    Union与Union All的区别 如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并 ...

  7. NPOI设置Excel单元格字体、边框、对齐、背景色

    代码: ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderBottom = BorderStyle.Thin; ce ...

  8. python中的循环和编码,运算符, 格式化输出

    1.while循环 现在让我们来看看python中的while循环  格式为 while 条件 循环体 (break) (continue) 中断循环的关键字有break和continue, brea ...

  9. java学习笔记—web计算器(36)

    MVC模式 模式主要的任务是帮助开发者解决一类问题. MVC模式主要是用于规划你的网站的开发的一个基本的结构. Servlet记住充当的是控制器层.cn.itcast.controller Java类 ...

  10. 【OCP-12c】2019年CUUG OCP 071考试题库(73题)

    73.Which statement correctly grants a system privilege? A. GRANT CREATE VIEW ON table1 TO user1; B. ...