-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path63.cpp
27 lines (27 loc) · 980 Bytes
/
63.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size(), n = obstacleGrid[0].size();
if (obstacleGrid[0][0]) return 0;
for(int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (obstacleGrid[i][j]) {
obstacleGrid[i][j] = 0;
} else {
if (i == 0 && j == 0) {
obstacleGrid[i][j] = 1;
} else if (i == 0) {
obstacleGrid[i][j] = obstacleGrid[i][j-1];
} else if (j == 0) {
obstacleGrid[i][j] = obstacleGrid[i-1][j];
} else {
obstacleGrid[i][j] = obstacleGrid[i][j-1] + obstacleGrid[i-1][j];
}
}
}
}
return obstacleGrid.back().back();
}
};