733. Flood Fill
T :O(mn)
S: O(2m+2n)
```java
class Solution {
private static final int[][] DIRS = {{0,1}, {1, 0}, {-1, 0}, {0, -1}};
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int m = image.length;
int n = image[0].length;
int initColor = image[sr][sc];
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{sr, sc});
while (!queue.isEmpty()) {
int[] cur = queue.poll();
if (image[cur[0]][cur[1]] == color) {
continue;
}
image[cur[0]][cur[1]] = color;
for (int[] dir : DIRS) {
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if (x < 0 || x >= m || y < 0 || y >= n || image[x][y] != initColor) {
continue;
}
queue.offer(new int[]{x, y});
}
}
return image;
}
}
```
Last updated
Was this helpful?