forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood-fill.cpp
27 lines (25 loc) · 889 Bytes
/
flood-fill.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
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int color = image[sr][sc];
if (color == newColor) return image;
dfs(&image, sr, sc, newColor, color);
return image;
}
private:
void dfs(vector<vector<int>> *image, int r, int c, int newColor, int color) {
static const vector<pair<int, int>> directions{{-1, 0}, { 1, 0},
{ 0, 1}, { 0, -1}};
if (r < 0 || r >= image->size() ||
c < 0 || c >= (*image)[0].size() ||
(*image)[r][c] != color) {
return;
}
(*image)[r][c] = newColor;
for (const auto& d : directions) {
dfs(image, r + d.first, c + d.second, newColor, color);
}
}
};