10000 add cpp solution · qilingit/Leetcode@721fa6e · GitHub
[go: up one dir, main page]

Skip to content

Commit 721fa6e

Browse files
committed
add cpp solution
1 parent 2f59eb3 commit 721fa6e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const int dx[4] = {-1, 0, 1, 0};
2+
const int dy[4] = {0, -1, 0, 1};
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
7+
int n = image.size();
8+
if (n == 0) return image;
9+
int m = image[0].size();
10+
11+
queue<pair<int,int>> q;
12+
q.push(make_pair(sr, sc));
13+
vector<vector<int>> vis(n, vector<int>(m, 0));
14+
vis[sr][sc] = 1;
15+
int c = image[sr][sc];
16+
image[sr][sc] = newColor;
17+
18+
while(!q.empty()){
19+
auto cur = q.front();
20+
q.pop();
21+
for (int i = 0; i < 4; i++){
22+
int x = cur.first + dx[i];
23+
int y = cur.second + dy[i];
24+
if (0 <= x and x < n and 0 <= y and y < m and !vis[x][y] and image[x][y] == c){
25+
q.push(make_pair(x, y));
26+
vis[x][y] = 1;
27+
image[x][y] = newColor;
28+
}
29+
}
30+
}
31+
return image;
32+
}
33+
};

0 commit comments

Comments
 (0)
0