-
Notifications
You must be signed in to change notification settings - Fork 0
/
0733. Flood Fill
32 lines (29 loc) · 901 Bytes
/
0733. Flood Fill
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
https://leetcode.com/problems/flood-fill/description/
Time: O(m*n)
Space: O(m*n)
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int old = image[sr][sc];
if (old != color) {
dfs(image, sr, sc, old, color);
}
return image;
}
private void dfs(int[][] image, int row, int col, int old, int color) {
if (image[row][col] == old) {
image[row][col] = color;
if (row >= 1) {
dfs(image, row - 1, col, old, color);
}
if (row < image.length - 1) {
dfs(image, row + 1, col, old, color);
}
if (col >= 1) {
dfs(image, row, col - 1, old, color);
}
if (col < image[0].length - 1) {
dfs(image, row, col + 1, old, color);
}
}
}
}