【leetcode刷题笔记】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.
题解:和http://www.cnblogs.com/sunshineatnoon/p/3798167.html非常相似,要注意两点:
- 有障碍的地方能走到的方法数目是0;
- 初始化结果矩阵的时候,如果第一行(列)的某个位置有障碍,那么这一行(列)该元素后面所有的元素都是0,不能置为1;比如给定障碍矩阵[1,0,0],那么初始化以后的矩阵应该为[0,0,0],而不是[0,1,1],因为(0,0)处有障碍,那么该矩阵的任何位置都是无法到达的。
代码如下:
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
if(m==0 && n == 0)
return 0; int[][] PathNum = new int[m][n];
for(int i = 0;i < m;i++)
if(obstacleGrid[i][0] != 1)
PathNum[i][0] = 1;
else
break;
for(int i = 0;i < n;i++)
if(obstacleGrid[0][i] != 1)
PathNum[0][i] = 1;
else
break; for(int i = 1;i < m;i++)
for(int j = 1;j < n;j++){
if(obstacleGrid[i][j] != 1)
PathNum[i][j] = PathNum[i-1][j]+PathNum[i][j-1];
} return PathNum[m-1][n-1];
}
}
【leetcode刷题笔记】Unique Paths II的更多相关文章
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷题笔记】Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode刷题笔记】N-Queens II
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 刷题62. Unique Paths
一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...
随机推荐
- 了解.net mvc实现原理ActionResult/View
了解.net mvc实现原理ActionResult/View 上一篇了解了请求至Controller的Action过程,这篇继续看源码处理Action收到请求数据再返回ActionResult到Vi ...
- tony_update yum
更改方法是这样的 在 /etc/yum.repos.d 下 1 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo 2 #mv Cen ...
- surface4 笔盖失灵的解决方案
http://tieba.baidu.com/p/3670357234 先找到设备管理器,找到蓝牙,删除里面所有的设备.然后重启. 之后再次找到蓝牙,匹配pen.就可以用了. 解决的前提是:我确定笔帽 ...
- MFC错误集锦
MFC中相关报错及其解决的方法: (1)0x00000005: 解决的方法:看是哪里的 数组越界: (2)0xCCCCCCCC:在类中声明指针,但没有赋初值之类的错误. 解决的方法:在类的构造函数中给 ...
- MySQL_使用时遇到的问题汇总
一.data too long for column 'name' at row 1 1.现象:把数据库的字符集编码设置为utf-8,通过DOS界面向表的某一列插入汉字时会遇到类似 data too ...
- linux系统寻找僵尸进程
1. 用top命令来查看服务器当前是否有僵尸进程. 2. 用ps和grep命令寻找僵尸进程 $ ps -A -ostat, pid, ppid, cmd | grep -e '^[Zz]' 命令解释: ...
- PHP性能之语言性能优化说明
PHP语言性能优化优化啥? 如下图所示,PHP直接执行的是opcode,所以我们尽量减少扫描和转码解析. 这是我们第一个优化点,尽量使用PHP内置的函数代替我们的代码来实现同样的功能. 和我们自己写的 ...
- LVS+Keepalived(DR模式)学习笔记
1.简述 在互联网的中型项目中,单服务器往往已经无法满足业务本身的性能要求,这时候就会平行扩展,把负载分摊到数台服务器上(集群).一般实现集群有DNS轮询,LVS,nginx负载均衡. 集群主要目的包 ...
- windows下XAMPP安装php_memcache扩展
windows下XAMPP安装php_memcache扩展 首先下载phpmemcache,地址为: http://up.2cto.com/2012/0522/20120522094758371.ra ...
- IOS程序国际化
1.1 新建一个Single View app模版项目,命名为Localization. 1.2 新建后,可以看到工作目录结构文件如下,单击InfoPlist.strings,查看右边的属性,在Loc ...