Create 63. Unique Paths II.md

This commit is contained in:
唐树森 2019-02-03 22:25:01 +08:00 committed by GitHub
parent 14bb1dea66
commit fefb66c963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,23 @@
# [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)
# 思路
这题其实和[62. Unique Paths](https://leetcode.com/problems/unique-paths/)几乎一样的,唯一区别就是矩形网格里存在一些障碍物,我们对每个格子判断一下就行了。
就是一个简单的动归,可参考[62题题解](https://github.com/ShusenTang/LeetCode/blob/master/solutions/62.%20Unique%20Paths.md)。
时间空间复杂度都是O(mn)
# C++
``` C++
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
vector<vector<int>>dp(m + 1, vector<int>(n + 1, 0));
dp[1][1] = 1;
for(int i = 1; i <= m; i++)
for(int j = 1; j <= n; j++){
if(obstacleGrid[i - 1][j - 1]) dp[i][j] = 0; // 此处为障碍物
else dp[i][j] += (dp[i - 1][j] + dp[i][j - 1]);
}
return dp[m][n];
}
};
```