-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood_fill_lc733.java
39 lines (37 loc) · 1.19 KB
/
flood_fill_lc733.java
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
class Solution {
int arR = 0;
int arC = 0;
boolean [][] visited;
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
this.arR = image.length;
this.arC = image[0].length;
visited = new boolean[arR][arC];
fill(image, sr, sc, newColor, image[sr][sc], true);
return image;
}
private boolean visited(int i, int j) {
if (visited[i][j] == true)
return true;
return false;
}
private void fill(int[][] image, int i, int j, int newColor, int oldColor, boolean input) {
if (i < 0 || i >= arR || j < 0 || j >= arC ) return;
if (visited(i,j) == true) return;
visited[i][j] = true;
if (input == true) {
image[i][j] = newColor;
}
else {
if (image[i][j] != oldColor) return;
image[i][j] = newColor;
}
// check up
fill (image, i-1, j, newColor, oldColor, false);
// check down
fill(image, i+1, j, newColor, oldColor, false);
// check left
fill(image,i, j-1, newColor, oldColor, false);
// check right
fill (image, i, j+1, newColor, oldColor, false);
}
}