leetcode — unique-paths-ii
/**
* Source : https://oj.leetcode.com/problems/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.
*
*/
public class UniquePath2 {
/**
* 依然使用动态规划
* 注意障碍,障碍在边上和中间
*
* @param maze
* @return
*/
public int finAllUniquePaths (int[][] maze) {
if (maze.length <= 0 || maze[0].length <= 0) {
return 0;
}
int max = 0;
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[0].length; j++) {
if (maze[i][j] == 1) {
// 障碍处为0
max = maze[i][j] = 0;
} else {
if (i > 0 && j > 0) {
max = maze[i][j] = maze[i-1][j] + maze[i][j-1];
} else if (i > 0) {
// 第一列不一定是1
max = maze[i][j] = maze[i-1][j];
} else if (j > 0) {
// 第一行不一定是1
max = maze[i][j] = maze[i][j-1];
} else {
// 第一个是1
max = maze[i][j] = 1;
}
}
}
}
return max;
}
public static void main(String[] args) {
UniquePath2 uniquePaths = new UniquePath2();
int[][] arr = new int[][]{
{0,1},
{0,0}
};
int[][] arr1 = new int[][]{
{0,1,0},
{0,0,0}
};
int[][] arr2 = new int[][]{
{0,1,0},
{0,1,0},
{0,0,0}
};
int[][] arr3 = new int[][]{
{0,0,0},
{0,1,0},
{0,0,0}
};
int[][] arr4 = new int[][]{
{0,0,0,0,0,0,0},
{0,1,0,0,0,0,0},
{0,0,0,0,0,0,0}
};
System.out.println(uniquePaths.finAllUniquePaths(arr));
System.out.println(uniquePaths.finAllUniquePaths(arr1));
System.out.println(uniquePaths.finAllUniquePaths(arr2));
System.out.println(uniquePaths.finAllUniquePaths(arr3));
System.out.println(uniquePaths.finAllUniquePaths(arr4));
}
}
leetcode — unique-paths-ii的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- Codeforces Round #555 (Div. 3) E. Minimum Array
题意:b数组可以自由排序,c[i]=(a[i]+b[i])%n. 题目中要求c数组的字典序是最小的.那么我们需要尽量满足前面的c[i],才能使字典序最小. 我们知道a[i]和b[i]都是[0,n-1] ...
- idea配置.gitignore后无法起作用
1)要先进入项目包所在的文件夹 2)git rm -r --cached . ://后面有个点3)git add . ;后面有个点4)git commit -m "update .giti ...
- Timer of STM32
下面是STM32得定时器程序,分两个文件Timer.c和Timer.h /*************************************************************** ...
- 学习Python第五天
今天咱们讲一下元组,hash,字典,元组是数据类型其中之一 元组的特性为有序的,不可变的,但是如果其中有可变元素,这些可变元组还是可以改变的,代码如下: 元组的写法:name = (‘roy’,‘al ...
- IDEA添加项目为library
1.如果要作为library的项目没有被IDEA编译就先把项目放在IDEA里面编译一下,因为这样后面导入module会方便一点.下面直接附上步骤 2.假设你已经用IDEA编译好了作为library的项 ...
- 算法第四版jar包下载地址
算法第四版jar包下载地址:https://algs4.cs.princeton.edu/code/
- MFC中线程相关知识
MFC中把线程分为两种类型,UI线程和工作者线程. MFC中启动一个线程的最好方法是调用AfxBeginThread,有两个版本,一个用于启动Ui线程,另外一个用于启动工作者线程.在MFC程序中,只有 ...
- 2018年1月20日上海MVP线下技术分享会纪实
1月20日正值大寒节气,在微软MVP朱兴亮的组织牵头下,上海MVP自发举办了题为<跟社区专家一起聊聊混合云.领域驱动.区块链和数字营销>的技术交流会.四名来自上海的MVP分别在自己擅长的技 ...
- Android插件化的兼容性(上):Android O的适配
首先声明,<Android插件化开发指南>这本书所介绍的Android底层是基于Android6.0(API level 23)的,而本书介绍的各种插件化解决方案,以及配套的70多个例子, ...
- Senparc.Weixin.TenPay 正式发布
微信支付刚出来的时候,和公众号的绑定关系很深(甚至旧版本使用的就是公众号的appId),随着微信生态的逐步丰富,微信支付越来越成为一个独立的平台,同时服务于公众号.小程序.开放平台.企业号/企业微信等 ...