【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 ...
随机推荐
- 6、DRN-----深度强化学习在新闻推荐上的应用
1.摘要: 提出了一种新的深度强化学习框架的新闻推荐.由于新闻特征和用户喜好的动态特性,在线个性化新闻推荐是一个极具挑战性的问题. 虽然已经提出了一些在线推荐模型来解决新闻推荐的动态特性,但是这些方法 ...
- [BZOJ3673&3674]可持久化并查集&加强版
题目大意:让你实现一个可持久化的并查集(3674强制在线). 解题思路:刚刚介绍了一个叫rope的神器:我是刘邦,在这两题(实际上两题没什么区别)就派上用场了. 正解应该是主席树||可持久化平衡树,然 ...
- servlet3.0 @webfilter 过滤顺序
Servlet3.0之前Filter过滤的顺序是由用户在web.xml中配置的顺序决定的,如下会先执行encodingFilter,再执行filter1. <filter> <dis ...
- 009实现一个算法来删除单链表中的一个结点,仅仅给出指向那个结点的指针(keep it up)
呵呵,这个题不能直接删除已知的结点.由于是单链表,不知道前驱,仅仅知道 后继结点,直接删除会使链表断开.只是我们能够删除已知结点的后继结点, 把后继结点的值赋值给已知结点. #include < ...
- SolrCloud怎样创建跟新索引信息
发送leader,由leader定位地址.然后写到shard上,同一时候copy到replicaton . 如图
- hadoop1.1.0的伪分布搭建步骤
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFuYnVyZW4wMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Java IO(二) 之 InputStream
源代码均以JDK1.8作为參考 前言: InputStream实现了两个接口Closeable和AutoCloseable: Closeable:JDK1.5中引入,Closeable接口中仅仅有一个 ...
- oracle新手随记10
1. unpivot注意点:select new_col from (select ename,job,to_char(sal) as sal,null as c from emp) ...
- lua实现大数运算
lua实现的大数运算,代码超短,眼下仅仅实现的加减乘运算 ------------------------------------------------ --name: bigInt --creat ...
- js中编码的处理
今天修bug的时候对编码问题进行了解决.发现js中有一个escape()这个函数处理编码的问题. 定义跟方法: 语法 escape(string)String:必需,要被转义或者编码的字符串. 返回值 ...