-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlood Fill
More file actions
46 lines (37 loc) · 1.09 KB
/
Copy pathFlood Fill
File metadata and controls
46 lines (37 loc) · 1.09 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
LEETCODE PROBLEM SOLVING
PROBLEM->FLOOD FILL
class Solution {
public int[][] floodFill(int[][] image,
int sr,
int sc,
int color) {
int originalColor = image[sr][sc];
if (originalColor == color) {
return image;
}
dfs(image, sr, sc, originalColor, color);
return image;
}
private void dfs(int[][] image,
int r,
int c,
int originalColor,
int newColor) {
int rows = image.length;
int cols = image[0].length;
if (r < 0 || c < 0 ||
r >= rows || c >= cols ||
image[r][c] != originalColor) {
return;
}
image[r][c] = newColor;
dfs(image, r + 1, c,
originalColor, newColor);
dfs(image, r - 1, c,
originalColor, newColor);
dfs(image, r, c + 1,
originalColor, newColor);
dfs(image, r, c - 1,
originalColor, newColor);
}
}