LeetCode-63. Unique Paths II

原创 2016年10月07日 15:30:13

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.


这道题和上一题十分相像。只不过加了几个障碍。只需要把障碍所在的点置为0即可。所需注意的就是障碍在最上和最左的边上以及起始点的情况。代码如下:

public class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid[0][0] == 1){
            return 0;
        }
		boolean flag = false;
		int m = obstacleGrid.length;
		int n = obstacleGrid[0].length;
		for(int i = 0; i < m; i++){
			if(flag == false){
				if(obstacleGrid[i][0] == 0){
					obstacleGrid[i][0] = 1;
				}else{
					obstacleGrid[i][0] = 0;
					flag = true;
				}
			}else{
				obstacleGrid[i][0] = 0;
			}
		}
		flag = false;
		for(int i = 1; i < n; i++){
			if(flag == false){
				if(obstacleGrid[0][i] == 0){
					obstacleGrid[0][i] = 1;
				}else{
					obstacleGrid[0][i] = 0;
					flag = true;
				}
			}else{
				obstacleGrid[0][i] = 0;
			}
		}
		for(int i = 1; i < m; i++){
			for(int j = 1; j < n; j++){
				if(obstacleGrid[i][j] == 0){
					obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
				}else{
					obstacleGrid[i][j] = 0;
				}
			}
		}
		return obstacleGrid[m-1][n-1];
    
    }
}



[LeetCode][Java] Unique Paths II

题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How...
  • Evan123mg
  • Evan123mg
  • 2015-07-17 09:59:08
  • 806

LeetCode 63 — Unique Paths II(C++ Java Python)

题目:http://oj.leetcode.com/problems/unique-paths-ii/ Follow up for "Unique Paths": Now consider if so...
  • dragon_dream
  • dragon_dream
  • 2014-02-25 22:19:29
  • 2140

63 Unique Paths II

题目链接:https://leetcode.com/problems/unique-paths-ii/题目:Follow up for "Unique Paths":Now consider if s...
  • ChilseaSai
  • ChilseaSai
  • 2015-10-17 15:31:45
  • 755

Unique Paths II -- LeetCode

原题链接: http://oj.leetcode.com/problems/unique-paths-ii/  这道题跟Unique Paths非常类似,只是这道题给机器人加了障碍,不是每次都有两个...
  • linhuanmars
  • linhuanmars
  • 2014-03-26 04:15:28
  • 10894

LeetCode -- Unique Paths II

LeetCode -- Unique Paths II
  • csharp25
  • csharp25
  • 2015-12-01 09:45:52
  • 545

leetCode 62.Unique Paths (唯一路径) 解题思路和方法

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
  • xygy8860
  • xygy8860
  • 2015-07-15 14:23:47
  • 886

LeetCode Unique Paths II

和Unique PathsI差不多,不过增加了一个判断条件,如果有障碍的话,那么这个格就填写0,表示0个路径。 不过做这道题还是有点麻烦的: 1 需要初始化动态规划法表的第一行 2 每次需要更新表的第...
  • kenden23
  • kenden23
  • 2013-12-17 08:17:35
  • 2729

【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】

【063-Unique Paths II(唯一路径问题II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Follow up for “Unique Paths”: ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-01 07:19:35
  • 1943

leetcode-63. Unique Paths II

#题目 Follow up for “Unique Paths”: Now consider if some obstacles are added to the grids. How ...
  • u011518120
  • u011518120
  • 2016-12-09 10:12:49
  • 133

Leetcode-63. Unique Paths II

前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN...
  • mcf171
  • mcf171
  • 2016-10-07 13:34:04
  • 81
收藏助手
不良信息举报
您举报文章:LeetCode-63. Unique Paths II
举报原因:
原因补充:

(最多只允许输入30个字)