8000 add kotlin/java solution for 733 Flood-Fill · qilingit/Leetcode@a1e9b35 · GitHub
[go: up one dir, main page]

Skip to content

Commit a1e9b35

Browse files
add kotlin/java solution for 733 Flood-Fill
1 parent c5541d4 commit a1e9b35

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
public class FloodFill733 {
2+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
3+
if (image[sr][sc] != newColor) {
4+
dfs(image, sr, sc, image[sr][sc], newColor);
5+
}
6+
return image;
7+
}
8+
// 0ms
9+
private void dfs(
10+
final int[][] image,
11+
final int x,
12+
final int y,
13+
final int target,
14+
final int newColor) {
15+
if (inBound(image, x, y) && image[x][y] == target) {
16+
image[x][y] = newColor;
17+
dfs(image, x - 1, y, target, newColor);
18+
dfs(image, x, y - 1, target, newColor);
19+
dfs(image, x + 1, y, target, newColor);
20+
dfs(image, x, y + 1, target, newColor);
21+
}
22+
}
23+
24+
private boolean inBound(
25+
final int[][] image,
26+
final int x,
27+
final int y) {
28+
return x >= 0 && y >= 0 && x < image.length && y < image[0].length;
29+
}
30+
31+
/*
32+
1ms
33+
public void dfs(int[][] image, int x, int y, int target, int newColor) {
34+
if (image[x][y] == target) {
35+
image[x][y] = newColor;
36+
if (x >= 1) {
37+
dfs(image, x - 1, y, target, newColor);
38+
}
39+
if (y >= 1) {
40+
dfs(image, x, y - 1, target, newColor);
41+
}
42+
if (x + 1 < image.length) {
43+
dfs(image, x + 1, y, target, newColor);
44+
}
45+
if (y + 1 < image[0].length) {
46+
dfs(image, x, y + 1, target, newColor);
47+
}
48+
}
49+
}
50+
*/
51+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
class FloodFillKotlin733 {
2+
3+
fun floodFill(image: Array<IntArray>, sr: Int, sc: Int, newColor: Int): Array<IntArray> {
4+
if (newColor != image[sr][sc]) {
5+
dfs(image, image[sr][sc], sr, sc, newColor)
6+
}
7+
return image
8+
}
9+
10+
// 320ms
11+
private fun dfs(
12+
image: Array<IntArray>,
13+
target: Int,
14+
x: Int,
15+
y: Int,
16+
newColor: Int
17+
) {
18+
if (inBound(image, x, y) && image[x][y] == target) {
19+
image[x][y] = newColor
20+
// UP
21+
dfs(image, target, x - 1, y, newColor)
22+
// LEFT
23+
dfs(image, target, x, y - 1, newColor)
24+
// BOTTOM
25+
dfs(image, target, x + 1, y, newColor)
26+
// RIGHT
27+
dfs(image, target, x, y + 1, newColor)
28+
}
29+
}
30+
31+
private fun inBound(image: Array<IntArray>, x: Int, y: Int): Boolean =
32+
x >= 0 && y >= 0 && x < image.size && y < image[0].size
33+
34+
/*
35+
228 ms
36+
private fun dfs(
37+
image: Array<IntArray>,
38+
target: Int,
39+
x: Int,
40+
y: Int,
41+
newColor: Int
42+
) {
43+
if (image[x][y] == target) {
44+
image[x][y] = newColor
45+
// UP
46+
if (x >= 1) {
47+
dfs(image, target, x - 1, y, newColor)
48+
}
49+
// LEFT
50+
if (y >= 1) {
51+
dfs(image, target, x, y - 1, newColor)
52+
}
53+
// BOTTOM
54+
if (x + 1 < image.size) {
55+
dfs(image, target, x + 1, y, newColor)
56+
}
57+
// RIGHT
58+
if (y + 1 < image[0].size) {
59+
dfs(image, target, x, y + 1, newColor)
60+
}
61+
}
62+
}
63+
*/
64+
}

0 commit comments

Comments
 (0)
0