【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】
【063-Unique Paths II(唯一路径问题II)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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
.
题目大意
唯一路径问题兴许。假设路径中有障碍,求总的路径的种数。
【唯一路径问题】
注意:网格的行数和列数都不超过100
解题思路
採用分治求解方法
用一个m*n的组数A保存结果。
对于A数组中的元素有。
1、当x=0或者y=0时。而且(x, y)位置无障碍。
有A[x][y] = 1。 有障碍就是0
2、当x>=1而且y>=1时,而且(x, y)位置无障碍。
有A[x][y] = A[x-1][y]+A[x][y-1]。 有障碍就是0
3、所求的结点就是A[m-1][n-1]。
代码实现
算法实现类
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
// 输入校验
if (obstacleGrid == null || obstacleGrid.length < 1 || obstacleGrid[0].length < 1
|| obstacleGrid[0][0] == 1
|| obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
return 0;
}
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
int[][] result = new int[rows][cols];
// 第一个位置有多少种方法。无障碍就是1种,有障碍就是0种
result[0][0] = obstacleGrid[0][0] == 0 ?
1 : 0;
for (int i = 1; i < cols; i++) {
result[0][i] = obstacleGrid[0][i] == 0 ? result[0][i - 1] : 0;
}
for (int i = 1; i < rows; i++) {
result[i][0] = obstacleGrid[i][0] == 0 ? result[i - 1][0] : 0;
}
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
result[i][j] = obstacleGrid[i][j] == 0 ?
result[i - 1][j] + result[i][j - 1] : 0;
}
}
return result[rows - 1][cols - 1];
}
// 使用递归方法会超时
public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
// 输入校验
if (obstacleGrid == null || obstacleGrid.length < 1 || obstacleGrid[0].length < 1
|| obstacleGrid[obstacleGrid.length - 1][obstacleGrid[0].length - 1] == 1) {
return 0;
}
int[] result = {0};
solve(obstacleGrid, 0, 0, result);
return result[0];
}
public void solve(int[][] grid, int row, int col, int[] sum) {
// 到达终点
if (row == grid.length - 1 && col == grid[0].length - 1) {
sum[0]++;
}
// 没有到终点,点在棋盘内。而且当前位置不是
else if (row >= 0 && row < grid.length && col >= 0 && col < grid[0].length && grid[row][col] == 0) {
// 往右走
solve(grid, row, col + 1, sum);
// 往下走
solve(grid, row + 1, col, sum);
}
}
}
评測结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47182723】
【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...
- LeetCode OJ:Unique Paths(唯一路径)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
[096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
- 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】
[136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...
随机推荐
- 浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
html_entity_decode():把html实体转换为字符. $str = "just atest & 'learn to use '"; echo html_en ...
- 强化学习(2)----Q-learning
1.Q-learning主要是Q表: 当前状态s1,接下来可以有两个动作选择,看电视a1和学习a2,对于agent人来说,可以根据reward来作出决策(Policy).目的就是得到奖励最大. Q-l ...
- indexedDB介绍
什么是 indexedDB IndexedDB 是一种使用浏览器存储大量数据的方法.它创造的数据可以被查询,并且可以离线使用. IndexedDB对于那些需要存储大量数据,或者是需要离线使用的程序是非 ...
- 紫书 例题 10-1 UVa 11582 (unsigned long long+模)
(1)这道题要用到 unsigned long long, 弄了我好久 这道题范围可以达到2的64次方-1, 而long long 最多到2的63次方-1, 而unsigned long long可以 ...
- 2016 10 26考试 NOIP模拟赛 杂题
Time 7:50 AM -> 11:15 AM 感觉今天考完后,我的内心是崩溃的 试题 考试包 T1: 首先看起来是个贪心,然而,然而,看到那个100%数据为n <= 2000整个人就虚 ...
- linux 下同步异步,堵塞非堵塞的一些想法
补充: 发现一个更好的解释样例:同步是一件事我们从头到尾尾随着完毕.异步是别人完毕我们仅仅看结果. 堵塞是完毕一件事的过程中可能会遇到一些情况让我们等待(挂起).非堵塞就是发生这些情况时我们跨过. 比 ...
- javascript jquery 推断对象为空的方式
java中存在非常多空指针的问题,须要常常做预防和推断,如若不然,控制台出现恼人的异常,让人信心备受打击,早期敲代码的时候没有经验,不能依据异常信息找到问题的根源,唯一做的事情就是祈祷,千万别出现什么 ...
- 【Unity】近期整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享。
近期整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享. 1:Unity4.x 项目中3D模型其材质丢失,成为"白模"? 解决方式:手 ...
- bzoj1800: [Ahoi2009]fly 飞行棋(乱搞)
1800: [Ahoi2009]fly 飞行棋 题目:传送门 题解: 大水题,早上签个到 没什么好说的...搞个前缀和,算个周长... 周长为奇数肯定误解啊废话QWQ 那么看到n<=20,还不暴 ...
- 31.ng-init 指令初始化 AngularJS 应用程序变量。
转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...