Flood Fill LeetCode solution in Java

1
class Solution {
2
void colorFill(int[][] image, int sr, int sc, int sourceColor, int targetColor) {
3
int m = image.length, n = image[0].length;
4

5
if (sr >= 0 && sr < m && sc >= 0 && sc < n) {
6
if ((image[sr][sc] != sourceColor) || (image[sr][sc] == targetColor)) return;
7

8
image[sr][sc] = targetColor;
9
colorFill(image, sr, sc - 1, sourceColor, targetColor); // left
10
colorFill(image, sr, sc + 1, sourceColor, targetColor); // right
11
colorFill(image, sr - 1, sc, sourceColor, targetColor); // up
12
colorFill(image, sr + 1, sc, sourceColor, targetColor); // down
13
} else return;
14
}
15

16
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
17
int rows = image.length, cols = image[0].length;
18

19
colorFill(image, sr, sc, image[sr][sc], color);
20
return image;
21
}
22
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0