forked from pengsida/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode63.cpp
37 lines (34 loc) · 971 Bytes
/
leetcode63.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
using namespace std;
class Solution {
private:
vector<vector<int>> ans;
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if (obstacleGrid.empty())
return 0;
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
ans = vector<vector<int>>(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
if (obstacleGrid[i][0])
break;
ans[i][0] = 1;
}
for (int j = 0; j < n; j++) {
if (obstacleGrid[0][j])
break;
ans[0][j] = 1;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (obstacleGrid[i][j])
continue;
if (i != 0 && j != 0)
ans[i][j] = ans[i - 1][j] + ans[i][j - 1];
}
}
return ans[m - 1][n - 1];
}
};