LeetCode 63. Unique Path II

10人阅读 评论(0) 收藏 举报
分类:

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.

public class Solution 
{
    public int uniquePathsWithObstacles(int[][] obstacleGrid) 
    {
        if(obstacleGrid[0][0] == 1 || 
                obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1] == 1) // obstacle at start point or finish point
            return 0;

        obstacleGrid[0][0] = -1; // start point
        
        for(int i=0; i<obstacleGrid.length; i++) // row
        {
            for(int j=0; j<obstacleGrid[0].length; j++) // column
            {
                // if this is not obstacle
                if(obstacleGrid[i][j] !=1)
                {
                    // get left: left is not obstacle 
                    if(j-1 >=0 && obstacleGrid[i][j-1] !=1)
                        obstacleGrid[i][j] += obstacleGrid[i][j-1];
                    // get top: top is not obstacle
                    if(i-1 >=0 && obstacleGrid[i-1][j] !=1)
                        obstacleGrid[i][j] += obstacleGrid[i-1][j];
                }
                
            }
        }
        
        return obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1] * -1;
    }
}

查看评论

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
  • 2139

LeetCode63:Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many uniqu...
  • u012501459
  • u012501459
  • 2015-06-12 16:05:00
  • 909

[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
  • 800

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

Leetcode 63:Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many uniqu...
  • geekmanong
  • geekmanong
  • 2016-02-24 00:15:36
  • 921

leetcode 63. Unique Paths II DP动态规划

Follow up for “Unique Paths”:Now consider if some obstacles are added to the grids. How many unique ...
  • JackZhang_123
  • JackZhang_123
  • 2017-09-05 10:32:21
  • 107

leetcode 62. Unique Paths-唯一路径|动态规划

Total Accepted: 80566 Total Submissions: 225855 Difficulty: Medium A robot is located at the top-...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-11 15:16:22
  • 1917

leetcode 63. Unique Paths II-唯一路径|动态规划

原文链接:Unique Paths II 【抛砖】 与Unique Paths不同的是,这里给机器人增加了障碍物,需要在递推式res[i][j]=res[i-1][j]+res[i][j-1]之前增加...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-11 20:53:57
  • 1117

[Leetcode] 63. Unique Path II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many uni...
  • u013897903
  • u013897903
  • 2015-03-24 10:20:04
  • 215

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
  • 885
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 1万+
    积分: 1368
    排名: 3万+
    博客专栏
    最新评论