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 ...
随机推荐
- linux 安装mysql5.7.25
这两天一直在弄mysql.一直安装.终于可以安装一个成一个了.哈哈哈 自己又写了个脚本希望对大家有所帮助 脚本非常简单 不错操作起来也很容易 重要提示 我的linux 是centos7.不是6. 7和 ...
- Win7 VS2017编译Audacity2.1.3
最近比较热衷折腾大型开源软件编译,因为在逐渐用开源软件替换盗版软件,除去盗版用着不安全的原因外,主要还是因为开源软件有源码,可以学习研究,另外就是体积小. 像Matlab每次装完都用不上什么功能,体积 ...
- k8s对接ceph存储
前提条件:已经部署好ceph集群 本次实验由于环境有限,ceph集群是部署在k8s的master节点上的 一.创建ceph存储池 在ceph集群的mon节点上执行以下命令: ceph osd pool ...
- VMware虚拟机Linux增加磁盘空间的扩容操作
转载自点击打开链接 用VMwareware虚拟机安装的Red Hat Enterprise Linux系统剩余空间不足,造成软件无法正常安装.如果重新装一遍系统就需要重新配置好开发环境和软件的安装配置 ...
- 现网环境业务不影响,但是tomcat启动一直有error日志,ERROR org.apache.catalina.startup.ContextConfig- Unable to process Jar entry [module-info.class] from Jar [jar:file:/home/iufs/apache-tomcat/webapps/iufs/WEB-INF/lib/asm
完整的错误日志信息: 2019-03-19 15:30:42,021 [main] INFO org.apache.catalina.core.StandardEngine- Starting Ser ...
- BFS总结
能够用 BFS 解决的问题,一定不要用 DFS 去做! 因为用 Recursion 实现的 DFS 可能造成 StackOverflow! (NonRecursion 的 DFS 一来你不会写,二来面 ...
- ionic3 百度地图插件定位 问题
每次用 cordova-pluin-baidumaploaction 每调用一次 他只会执行一次 我想循环 但是每次都会初始化 把插件的java代码98行注释就好了
- java(一) 基础部分
1.11.简单讲一下java的跨平台原理 Java通过不同的系统.不同版本.不同位数的java虚拟机(jvm),来屏蔽不同的系统指令集差异而对外体统统一的接口(java API),对于我们普通的jav ...
- python 从基础到入门链接
机器学习篇: 先看的 简书 木子昭的机器学习三剑客 : https://www.jianshu.com/u/c5d047065c42 然后看完之后又发现一个很好的链接, nkwy2012博主提供了很多 ...
- ByteArrayInputStream
package org.example.io; import java.io.ByteArrayInputStream;import java.io.IOException; /** * ByteAr ...