【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 ...
随机推荐
- layero和index
zIndex:layer.zIndex, success:function(index,layero){ //layero 为当前层的DOM对象 var zIndex = layer.index; $ ...
- Python2.* object类............
class object: """ The most base type """ def __delattr__(self, name): ...
- linux常用命令技巧
原文地址 这篇文章来源于Quroa的一个问答<What are some time-saving tips that every Linux user should know?>—— Li ...
- 紫书 例题8-18 UVa 1442 (扫描法)
从左往右扫描一遍, 得从每个位置往右伸长不会碰到天花板的高度, 右往左一样, 取最小, 然后就是可以放"水"的高度了 #include<cstdio> #include ...
- 紫书 例题8-16 UVa 1608 (递归)
题意: 判断所给序列是否满足任意连续子序列中至少有一个出现一次的元素. 思路:在整体中找到一个只出现一次的元素, 然后在递归两边.因为两边的序列中有这个数那就满足要求, 所以就看剩下的序列漫步满足要求 ...
- Java基础学习总结(7)——Object类
一.Object类介绍 Object类在JAVA里面是一个比较特殊的类,JAVA只支持单继承,子类只能从一个父类来继承,如果父类又是从另外一个父类继承过来,那他也只能有一个父类,父类再有父类,那也只能 ...
- 基于json数据格式实现的简单数据库——jsonDB
已在github上建立项目:https://github.com/ThinkerCodeChina/jsonDB /** +-------------------------------------- ...
- layDate1.0正式公布,您一直在寻找的的js日期控件
你是时候换一款日期控件了,而layDate很愿意和您成为工作伙伴.她正致力于成为全球最高大上的web日期支撑,为国内外全部从事web应用开发的同仁提供力所能及的动力.她基于原生JavaScript精心 ...
- [Angular + TsLint] Disable directive selector tslint error
@Directive({ // tslint:disable-next-line:directive-selector selector: '[scrollable]' })
- linux清除邮件队列
[root@localhost mail]#tmp=`mailq | grep -E "root" | awk '{print $1}'` [root@localhost mail ...