leetcode 63. 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.
分析:
题中给出了m×n的方格,m和n最大值为100,左上角的小机器人只能每次向右走一步或者向下走一步,终点在方格的右下角,
求一共有多少条不同的路径。
最优的子结构: a[i,j] = a[i-1,j] + a[i,j-1]
第一行中,不管到达哪个点,都是沿着第一行一直走才能达到的,如果第一行的值为0,第一行的点的路径为1, a[i][0] = 1;
如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。
第一列中,不管到达哪个点,都是沿着第一列一直走才能达到的,如果方格内的值为0,第一行的点的路径为1, a[0][j] = 1;
如果遇到了障碍物,为1的后面的方格都走不到了,所以跳出循环。
从起点到方格内某个点的不同路径数量可以分解为该点上方的点和左侧的点路径之和。
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0){
return 0;
}
int n = obstacleGrid.length;
int m = obstacleGrid[0].length;
int[][] a = new int[n][m];
for(int i=0;i<n;i++){
if(obstacleGrid[i][0] == 0){
a[i][0] = 1;
}else{
break;
}
}
for(int j=0;j<m;j++){
if(obstacleGrid[0][j] == 0){
a[0][j] = 1;
}else{
break;
}
}
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(obstacleGrid[i][j] != 1){
a[i][j] = a[i-1][j] + a[i][j-1];
}else{
a[i][j] = 0;
}
}
}
return a[n-1][m-1];
}
}
leetcode 63. Unique Paths II的更多相关文章
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
- [leetcode] 63. Unique Paths II (medium)
原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
随机推荐
- 随便聊聊 SOA & SOAP & WebService 的一些东西,以及客户端开发的代码逻辑解析
http://blog.csdn.net/hikaliv/article/details/6459779 一天的时间调通了一个 WebService 的 Java 端的 C/S.一个 Android ...
- JavaScript中的变量及数据类型
转自:http://blog.csdn.net/mygis2005/article/details/7375419 JavaScript是一种弱类型的语言,变量名.操作符和方法名都区分大小写. 1.变 ...
- Myeclipse如何设置字体大小
由于Myeclipse一般是英文版的,这就给英语不太好的人带来了一定的麻烦,有时连设置个字体都无法顺利进行!!! 工具/原料 Myeclipse 方法/步骤 双击启动Myeclipse 点击& ...
- Linux学习笔记(一)2015.4.13
研究生由单片机转Linux学习 首先安装VMware虚拟机,用的是VMware 10.0 在VMware 10.0上安装视频上推荐的Red Hat Linux 5 安装后正式进入Linux学习 笔记1 ...
- Ubuntu——"xxx is not in the sudoers file.This incident will be reported" 错误解决方法
Ubuntu下普通用户用sudo执行命令时报如题所示错误,解决方法就是在/etc/sudoers文件里给该用户添加权限.如下: 1.切换到root用户下 2./etc/sudoers文件默认是只读的, ...
- ecshop 后台-》广告
1.后台广告宽度限制不能超过1024,高度大于1,admin/ad_position.php 第236行 || $ad_width < ) { make_json_error($_LANG['w ...
- JS中的 公有变量、私有变量 !
公有变量.私有变量 ! 初学者的见解,算是记录学习过程,也算是分享以便共同成长,如有不正确的地方,还请不吝赐教! 先看代码1: function car(){ var wheel = 3; //私有变 ...
- C++中const 的各种用法
C++中const 关键字的用法 const修饰变量 const 主要用于把一个对象转换成一个常量,例如: ; size = ; // error: assignment of read-only v ...
- Joomla![1.5-3.4.5]反序列化远程代码执行EXP(直接写shell)
Usage:x.py http://xxx.com # coding=utf-8# author:KuuKi# Help: joomla 1.5-3.4.5 unserialize remote co ...
- Win8 删除桌面右键中的显卡选项
打开注册表 regedit.exe HKEY_CLASSES_ROOT Directory Background shellex ContextMenuHandlers 按照上边的路径找过去.. 删除 ...