LeetCode: Unique Paths II 解题报告

时间:2022-12-21 07:58:44

Unique Paths II

Total Accepted: 31019 Total Submissions: 110866My Submissions

Question Solution 

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.

SOULUTION 1:

LeetCode: Unique Paths 解题报告 相比,只不过是判断一下当前是不是block,如果是block,直接设置D[i][j] 是0就好了 也就是不可达。 同样在3分钟之内Bug free 秒答,哦耶!

 public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
//
if (obstacleGrid == null || obstacleGrid.length == || obstacleGrid[].length == ) {
return ;
} int rows = obstacleGrid.length;
int cols = obstacleGrid[].length; int[][] D = new int[rows][cols]; for (int i = ; i < rows; i++) {
for (int j = ; j < cols; j++) {
D[i][j] = ;
if (obstacleGrid[i][j] == ) {
D[i][j] = ;
} else {
if (i == && j == ) {
D[i][j] = ;
} if (i != ) {
D[i][j] += D[i - ][j];
} if (j != ) {
D[i][j] += D[i][j - ];
}
}
}
} return D[rows - ][cols - ];
}
}

LeetCode: Unique Paths II 解题报告的更多相关文章

  1. 【LeetCode】63&period; Unique Paths II 解题报告(Python & C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. LEETCODE —— Unique Paths II &lbrack;动态规划 Dynamic Programming&rsqb;

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

  4. LEETCODE —— Unique Paths II &lbrack;Dynamic Programming&rsqb;

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

  5. &lbrack;leetcode&rsqb;Unique Paths II &commat; Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. &lbrack;LeetCode&rsqb; Unique Paths II 不同的路径之二

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

  7. Leetcode Unique Paths II

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

  8. &lbrack;Leetcode&rsqb; unique paths ii 独特路径

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

  9. 【LeetCode】980&period; Unique Paths III解题报告(C&plus;&plus;)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

随机推荐

  1. android开发 兵器

    spring for android andriod anotatons 按android原生的方式写代码,会导致冗余,代码丑陋,开发效率低下. 最近对项目代码进行一些梳理和改进.

  2. python os模块

    import os path = "路径" os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname&quot ...

  3. 权限获取异常(不能用ModuleId,得换个名字)目前还没搞清楚为啥

    CenterController: /// <summary> /// 访问模块,写入系统菜单Id /// </summary> /// <param name=&quo ...

  4. AngularJs 返回上一页

    html <script src="lib/angular/angular-1.4.9/angular.js"></script> <script s ...

  5. part 设置

    RCC_AHBENR 中的17.18.19.20. 分别是PORTA,PORTB.PORTC.PORTD 的时钟使能控制位,要A.B.C.D 端口有效,置位. 如: RCC -> AHBENR  ...

  6. gb2312&comma;gbk&comma;utf8的区别

    GB2312编码大约包含6000多汉字(不包括特殊字符),编码范围为第一位b0-f7,第二位编码范围为a1-fe(第一位为cf时,第二位为a1-d3),计算一下汉字个数为6762个汉字.当然还有其他的 ...

  7. STM32F103 ------ 时钟配置

    由于stm32的库默认是外部晶振8M的情况下实现的,所以配置波特率的时候也是按8M,包括主频,如果用12M晶振就需要改动几个地方: 在system_stm32f10x.c中找到相应类型的文件,进行如下 ...

  8. Path for IClasspathEntry must be absolute&colon;

    关掉eclipse, 删除workspace工作目录下面的.metadata文件,看不到.metadata文件就 "ctril + H" 就可以看到了.然后重新打开eclipse, ...

  9. Source Code Pro 编程字体

    Source Code Pro :是 Adobe 公司号称最佳的编程字体,而且还是开源的 它非常适合用于阅读代码,支持 Linux.Mac OS X 和 Windows 等操作系统,而且无论商业或个人 ...

  10. python五十六课——正则表达式(常用函数之search&lpar;&rpar;)

    函数:search(regex,string,[flags=0]):参数:和match一样理解功能:从头开始匹配字符串中的数据,如果头不匹配继续往后尝试匹配,直到有第一个匹配成功的子数据,立即返回一个 ...