[LeetCode] Unique Paths II
2014-10-23 13:39
316 查看
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
in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
Note: m and n will be at most 100.
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
1and
0respectively
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.
class Solution { public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { int m = obstacleGrid.size(), n = obstacleGrid[0].size(); vector<vector<int>> f(m, vector<int>(n, 0)); for(int i = 0; i < m; i++) { if(obstacleGrid[i][0] == 0) f[i][0] = 1; else break; } for(int i = 0; i < n; i++) { if(obstacleGrid[0][i] == 0) f[0][i] = 1; else break; } for(int i = 1; i < m; i++) { for(int j = 1; j < n; j++) { if(obstacleGrid[i][j] == 0) f[i][j] = f[i-1][j] + f[i][j-1]; else f[i][j] = 0; } } return f[m-1][n-1]; } };
相关文章推荐
- 【LeetCode】Unique Paths II
- Leetcode Unique Paths II
- leetcode-62&63 Unique Paths I & II
- LeetCode: Unique Paths II [062]
- Java for LeetCode 063 Unique Paths II
- leetcode_Unique Paths II
- Unique Paths II - LeetCode
- 【LeetCode】Unique Paths II
- Unique Paths II -- LeetCode
- Leetcode_unique-paths-ii
- LeetCode 063 Unique Paths II
- leetcode - 62,63. Unique Paths(II) & 64.Minimum Path Sum
- [LeetCode][JavaScript]Unique Paths II
- LeetCode---Unique Paths II
- Leetcode: Unique Paths II
- Leetcode-Unique Paths II
- [LeetCode] Unique Paths II 解题报告
- 【leetcode】Array——Unique Paths II(63)
- LeetCode63 Unique Paths II
- LeetCode_63---Unique Paths II