-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloodFill.cpp
More file actions
24 lines (22 loc) · 857 Bytes
/
floodFill.cpp
File metadata and controls
24 lines (22 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Source: https://leetcode.com/problems/flood-fill/
// Author: Miao Zhang
// Date: 2021-03-05
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) return image;
floodFill(image, sr, sc, image[sr][sc], newColor);
return image;
}
private:
void floodFill(vector<vector<int>>& image, int i, int j, int orgColor, int newColor) {
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != orgColor) {
return;
}
image[i][j] = newColor;
floodFill(image, i + 1, j , orgColor, newColor);
floodFill(image, i - 1, j , orgColor, newColor);
floodFill(image, i, j + 1, orgColor, newColor);
floodFill(image, i, j - 1, orgColor, newColor);
}
};