-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
37 lines (30 loc) · 1.03 KB
/
Copy pathSolution.java
File metadata and controls
37 lines (30 loc) · 1.03 KB
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
28
29
30
31
32
33
34
35
36
37
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int n = image.length, m = image[0].length;
boolean[][] visited = new boolean[n][m];
Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{sr, sc});
while(!queue.isEmpty()) {
int[] curr = queue.poll();
int x = curr[0], y = curr[1];
if (visited[x][y]) {
continue;
}
if (x > 0 && image[x - 1][y] == image[x][y]) {
queue.add(new int[]{x-1, y});
}
if (y > 0 && image[x][y-1] == image[x][y]) {
queue.add(new int[]{x, y-1});
}
if (x < n - 1 && image[x + 1][y] == image[x][y]) {
queue.add(new int[]{x+1, y});
}
if (y < m - 1 && image[x][y+1] == image[x][y]) {
queue.add(new int[]{x, y+1});
}
image[x][y] = color;
visited[x][y] = true;
}
return image;
}
}