-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFloodFill.java
More file actions
48 lines (38 loc) · 1.32 KB
/
FloodFill.java
File metadata and controls
48 lines (38 loc) · 1.32 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
38
39
40
41
42
43
44
45
46
47
48
/**
* LeetCode 733 https://leetcode.com/problems/flood-fill/
*/
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (image == null || image.length == 0 || image[0].length == 0 || image[sr][sc] == newColor) {
return image;
}
int row = image.length;
int col = image[0].length;
Queue<int[]> q = new LinkedList<>();
int oldColor = image[sr][sc];
q.offer(new int[]{sr,sc});
while(!q.isEmpty()) {
int[] cur = q.poll();
int i = cur[0];
int j = cur[1];
image[i][j] = newColor;
// check up
if (i-1 >= 0 && image[i-1][j] == oldColor) {
q.offer(new int[]{i-1, j});
}
// check down
if (i+1 < row && image[i+1][j] == oldColor) {
q.offer(new int[]{i+1, j});
}
// check left
if (j-1 >= 0 && image[i][j-1] == oldColor) {
q.offer(new int[]{i, j-1});
}
// check right
if (j+1 < col && image[i][j+1] == oldColor) {
q.offer(new int[]{i, j+1});
}
}
return image;
}
}