/**
* 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. linux 启动weblogic重定向日志

    命令启动 nohup ./startWebLogic.sh  会默认输出nohup.out日志文件 时间久了日志文件会很大,占用空前(正常项目会自己处理日志输出,不需要用到nohup的默认输出日志) ...

  2. MySQL事物管理

    sql语句对数据库操作构成事物. 事物的特性:ACID •Atomicity(原子性):一个事物要么全都被执行,要么全都不执行,不会存在执行结束在中间环节.如果事物在执行过程中发生异常,则会回滚到事物 ...

  3. 2019.03.25 bzoj4539: [Hnoi2016]树(主席树+倍增)

    传送门 题意:给一棵大树,令一棵模板树与这棵树相同,然后进行mmm次操作,每次选择模板树中的一个节点aaa和大树中一个节点bbb,把aaa这棵子树接在bbb上面,节点编号顺序跟aaa中的编号顺序相同. ...

  4. LOJ-10092(最大半连通子图)

    题目连通:传送门 思路: 题目定义很清晰,然后就不会了QAQ…… 后来看了书,先缩点,然后再用拓扑排序找到最长的链子的节点数(因为缩点后所有点都是一个强连通分量,所以找最长的链子就是最大限度包含 点的 ...

  5. 使用proxyTable解决vue里的跨域问题

    由于没有跨域的接口,所以,用8080端口请求8081端口,来模拟跨域.跨域会出现下面报错. 1.找到config文件夹下index.js,在proxyTable对象里复制以下代码: proxyTabl ...

  6. Linux下好用的屏幕录像软件kazam及截图软件shutter

    都是apt直接安装即可使用. 其中kazam默认保存的文件格式是avi,非常大,通常录制几十秒就已经好几个G,导致录制过程太占用资源,会出现卡顿的现象. 在“首选项”中可以选择输出格式为mp4,文件就 ...

  7. vue学习-自动行合并的table

    测试的效果 测试的html源码截图 v-table在tableGroup.js中定义,以下就render方法,行的所有单元格都在tableGrouper.js中处理 render:function(h ...

  8. Forward团队-爬虫豆瓣top250项目-项目总结

    托管平台地址:https://github.com/xyhcq/top250 小组名称:Forward团队 组长:马壮 成员:李志宇.刘子轩.年光宇.邢云淇.张良 我们这次团队项目内容是爬取豆瓣电影T ...

  9. ubuntu自带截图工具--方便好用(转)

    一般用到的截图类型有三种:全屏.当前活动窗口.自定义区域,其中自定义区域截图是最灵活也是我们用的最多的方式.在ubuntu下可以通过其自带的截图工具轻松实现这三种功能. ubuntu自带的截图工具为s ...

  10. html基础知识梳理

    1.浏览器内核 补充:blink为Google与Opera共同开发. 2.web标准 web标准为w3c和其他标准化组织制定的一系列标准的集合.(标签闭合.小写.不乱嵌套,使用外链css/js;结构行 ...