-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path179-Flood Fill
More file actions
30 lines (28 loc) · 951 Bytes
/
Copy path179-Flood Fill
File metadata and controls
30 lines (28 loc) · 951 Bytes
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
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int m=image.length;
int n=image[0].length;
int pixel=image[sr][sc];
if (pixel == color) return image;
int dirX[]=new int[]{1,-1,0,0};
int dirY[]=new int[]{0,0,1,-1};
Queue<int[]> q=new LinkedList<>();
q.add(new int[]{sr,sc});
image[sr][sc] = color;
while(!q.isEmpty()){
int curr[]=q.poll();
int x=curr[0];
int y=curr[1];
for(int d=0;d<4;d++){
int nx=x+dirX[d];
int ny=y+dirY[d];
if(nx>=0 && ny>=0 && nx<m && ny<n && image[nx][ny]==pixel){
image[nx][ny]=color;
q.add(new int[]{nx,ny});
}
}
}
return image;
}
}
https://leetcode.com/problems/flood-fill/