forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
25 lines (24 loc) · 838 Bytes
/
solution.h
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
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using std::vector; using std::prev;
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
if (obstacleGrid.empty()) return 0;
vector<int> vec(obstacleGrid.front().cbegin(), obstacleGrid.front().cend());
auto flag = std::find(vec.begin(), vec.end(), 1);
std::fill(vec.begin(), flag, 1);
std::fill(flag, vec.end(), 0);
for (auto line = std::next(obstacleGrid.cbegin()); line != obstacleGrid.cend(); ++line) {
auto iter = vec.begin();
for (auto i : *line) {
if (i) *iter = 0;
else if (iter != vec.begin()) *iter += *prev(iter);
++iter;
}
}
return vec.back();
}
};