【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)】的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

    [107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...

  2. 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 ...

  3. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

  4. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  5. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  6. 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】

    [096-Unique Binary Search Trees(唯一二叉搜索树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given n, how many s ...

  7. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  8. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  9. 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】

    [136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...

随机推荐

  1. java zyUpload 实现多文件上传

    1.html部分 <form enctype="multipart/form-data"> <label>请选择文件</label> <i ...

  2. LINUX命令LS -AL 解析

    LINUX命令LS -AL 解析 linux命令ls -al 解析 ls是“list”的意思,与早期dos的命令dir功能类似.参数-al则表示列出所有的文件,包括隐藏文件,就是文件前面第一个字符为. ...

  3. 开源 PHP 项目找队友、顾问、指导、贡献者等等

    介绍站点还没做,先直接甩代码链接了 https://github.com/litphp/litphp Lit是什么? Lit是我一直在撸的个人框架,按第一次上传代码来说历史 超过4年 了,从还能支持P ...

  4. HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

    本题又是一题单调队列题解. 技巧就是须要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tai ...

  5. hdoj-5099-Comparison of Android versions

    Comparison of Android versions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  6. 数学之路-python计算实战(6)-numpy-ndarray

    >>>> mya=np.zeros(shape=(2,2)) >>>> mya array([[ 0.,  0.], [ 0.,  0.]]) > ...

  7. css 清楚浮动的8种方式

    清除浮动是每个 web前台设计师必须掌握的机能. css清除浮动大全,共8种方法. 浮动会使当前标签产生向上浮的效果,同一时候会影响到前后标签.父级标签的位置及 width height 属性.并且相 ...

  8. extjs Combox 调用数据

    1方法一 从 json获取 var typeStore = new Ext.data.Store({                 proxy : new Ext.data.HttpProxy({u ...

  9. Dictionary as a set of counters

    Suppose you are given a string and you want to count how many times each letters appears. There are ...

  10. POJ 3145 线段树 分块?+暴力

    思路: 线段树 (分类讨论) 此题数据很水 数据很水 数据很水 但是卡个暴力还是没问题的-- //By SiriusRen #include <cstdio> #include <c ...