-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (53 loc) · 1.57 KB
/
Copy pathSolution.java
File metadata and controls
65 lines (53 loc) · 1.57 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.example.problems.flood_fill;
import org.example.problems.SolutionInterface;
import java.util.LinkedList;
import java.util.Queue;
public class Solution implements SolutionInterface {
private Queue<Integer[]> q;
private int oldColor;
private int rows;
private int cols;
private int[][] matrix;
@Override
public String getName() {
return "Flood Fill";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/flood-fill/";
}
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
matrix = image;
oldColor = image[sr][sc];
rows = image.length;
cols = image[0].length;
if (oldColor == newColor || rows * cols == 0) {
return image;
}
q = new LinkedList<>();
q.add(new Integer[]{sr,sc});
while (q.size() > 0) {
Integer[] coords = q.poll();
int currentRow = coords[0];
int currentCol = coords[1];
image[currentRow][currentCol] = newColor;
enqueue(currentRow - 1, currentCol);
enqueue(currentRow + 1, currentCol);
enqueue(currentRow, currentCol + 1);
enqueue(currentRow, currentCol - 1);
}
return image;
}
private void enqueue(int r, int c) {
if (r < 0 || r >= rows) {
return;
}
if (c < 0 || c >= cols) {
return;
}
if (matrix[r][c] != oldColor) {
return;
}
q.add(new Integer[]{r, c});
}
}