[LeetCode]20. Unique Paths II唯一路径

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。注意初始化不能再是path[i][0]=1,path[0][j]=1(0<=i<m,0<=j<n),而是path[0][0]=1,因为第一行和第一列上就有可能有障碍。

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int res = 0;
        if(obstacleGrid.empty() || obstacleGrid[0].empty())
            return res;
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();

        vector<int> path(n, 0);
        path[0] = 1;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(obstacleGrid[i][j] == 1)
                    path[j] = 0;
                else if(j > 0)
                    path[j] += path[j - 1];
            }
        }

        res = path[n - 1];
        return res;
    }
};
时间: 2024-10-08 10:17:23

[LeetCode]20. Unique Paths II唯一路径的相关文章

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

Unique Paths II 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 obsta

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). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II 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

【Leetcode】Unique Paths II

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 middl

[LeetCode][JavaScript]Unique Paths II

Unique Paths II 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 obsta

【LeetCode】Unique Paths II 解题报告

[题目] 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

Leetcode 动态规划 Unique Paths II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths II Total Accepted: 13655 Total Submissions: 49081My Submissions Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would t

[C++]LeetCode: 78 Unique Paths II

题目: 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 m

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 robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t