mirror of https://github.com/doocs/leetcode.git
25 lines
843 B
Java
25 lines
843 B
Java
class Solution {
|
|
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
|
|
if (image[sr][sc] == newColor) {
|
|
return image;
|
|
}
|
|
Deque<int[]> q = new ArrayDeque<>();
|
|
q.offer(new int[] {sr, sc});
|
|
int oc = image[sr][sc];
|
|
image[sr][sc] = newColor;
|
|
int[] dirs = {-1, 0, 1, 0, -1};
|
|
while (!q.isEmpty()) {
|
|
int[] p = q.poll();
|
|
int i = p[0], j = p[1];
|
|
for (int k = 0; k < 4; ++k) {
|
|
int x = i + dirs[k], y = j + dirs[k + 1];
|
|
if (x >= 0 && x < image.length && y >= 0 && y < image[0].length
|
|
&& image[x][y] == oc) {
|
|
q.offer(new int[] {x, y});
|
|
image[x][y] = newColor;
|
|
}
|
|
}
|
|
}
|
|
return image;
|
|
}
|
|
} |