-
Notifications
You must be signed in to change notification settings - Fork 0
/
200107-1.cpp
57 lines (55 loc) · 1.04 KB
/
200107-1.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://leetcode-cn.com/problems/unique-paths-ii/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
if (m <= 0) return 1;
int n = obstacleGrid[0].size();
if (n <= 0) return 1;
vector<long int> a(n);
a[0] = obstacleGrid[0][0] ? 0 : 1;
for (int i = 1; i < n; ++i) {
if (obstacleGrid[0][i]) break;
a[i] = a[i - 1];
}
for (int j = 1; j < m; ++j) {
for (int i = 0; i < n; ++i) {
if (obstacleGrid[j][i]) {
a[i] = 0;
} else if (i > 0) {
a[i] += a[i - 1];
}
}
}
return a.back();
}
};
int main()
{
Solution s;
{
vector<vector<int>> a = {
{0,0,0},
{0,1,0},
{0,0,0}
};
printf("%d\n", s.uniquePathsWithObstacles(a)); // answer: 2
}
{
vector<vector<int>> a = {
{1},
};
printf("%d\n", s.uniquePathsWithObstacles(a)); // answer: 0
}
{
vector<vector<int>> a = {
{0},
{1},
};
printf("%d\n", s.uniquePathsWithObstacles(a)); // answer: 0
}
return 0;
}