/**
* 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的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

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

  3. LEETCODE —— Unique Paths II [Dynamic Programming]

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

  4. [LeetCode] Unique Paths II 不同的路径之二

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

  5. [leetcode]Unique Paths II @ Python

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

  6. Leetcode 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. [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 ...

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  10. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. python 实现rsa 的加密解密存读取(PEM格式证书)【转发】

    来源:CSDN 原文:https://blog.csdn.net/sjt1996/article/details/83377800

  2. mysql 模糊查询条件带‘%’问题

  3. 20155205 郝博雅 Exp3 免杀原理与实践

    20155205 郝博雅 Exp3 免杀原理与实践 一.基础问题回答 (1)杀软是如何检测出恶意代码的? 答:++基于特征码的检测++<简单来说一段特征码就是一段或多段数据.如果一个可执行文件( ...

  4. REdis主挂掉后复制节点才起来会如何?

    结论: 这种情况下复制节点(即从节点)无法提升为主节点,复制节点会一直尝试和主节点建立连接,直接成功.主节点恢复后,复制节点仍然保持为复制节点,并不会成为主节点. 复制节点无法提升为主节点的原因是复制 ...

  5. kali 日志

    MAC协议安全攻防 kali 攻击 输入 macof -i eth0 -i 选择网卡 防御 使用交换机的安全特性 Port Security DCRS 需要开启 mac地址表学习使用cpu控制 mac ...

  6. Docker构建其它组件

    构建mysql 运行centos7容器 docker run --privileged -dti --name=centos-container centos:7 /usr/sbin/init 查询c ...

  7. 2018/9/6 spring框架的整理

    spring知识的巩固整理AOP和ioc概念,以及了解到了为何要使用spring框架的目的,作用:变换资源获取的方向.更像是按需所求.配置bean的方式:利用XML的方式,基于注解的方式两种.1通过全 ...

  8. [转] Java 的泛型擦除和运行时泛型信息获取

    原文链接 https://my.oschina.net/lifany/blog/875769 前言 现在很多程序员都会在简历中写上精通 Java.但究竟怎样才算是精通 Java 呢?我觉得不仅要熟练掌 ...

  9. 关于iOS与html交互,隐藏或修改html标签内容

    wkwebview 1.隐藏顶部标题栏 [webView evaluateJavaScript:@"document.getElementsByClassName('page-header' ...

  10. 福州首届.NET开源社区技术交流会圆满成功

    活动总结 2018年11月10日周六的下午,在福州蒲公英创新工场举办了福州首届.NET开源社区技术交流会,来自福建省各大科技公司的技术小伙伴齐聚一堂,为了就是能在现场学习到微软跨平台技术.NET Co ...