Skip to content

Commit 9f3885f

Browse files
committed
feat: add the solution of Flood Fill(733) with Java.
1 parent ae7d6ad commit 9f3885f

File tree

3 files changed

+83
-24
lines changed

3 files changed

+83
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
| [720][720-question] | [Longest Word in Dictionary][720-tips] | [][720-java] | [][720-js] | |
8484
| [724][724-question] | [Find Pivot Index][724-tips] | [][724-java] | [][724-js] | |
8585
| [728][728-question] | [Self Dividing Numbers][728-tips] | [][728-java] | [][728-js] | |
86-
| [733][733-question] | [Flood Fill][733-tips] | | [][733-js] | |
86+
| [733][733-question] | [Flood Fill][733-tips] | [][733-java] | [][733-js] | |
8787
| [744][744-question] | [Find Smallest Letter Greater Than Target][744-tips] | | [][744-js] | |
8888
| [746][746-question] | [Min Cost Climbing Stairs][746-tips] | | [][746-js] | |
8989
| [747][747-question] | [Largest Number At Least Twice of Others][747-tips] | | [][747-js] | |
@@ -481,6 +481,7 @@
481481
[720-java]: ./src/_720/Solution.java
482482
[724-java]: ./src/_724/Solution.java
483483
[728-java]: ./src/_728/Solution.java
484+
[733-java]: ./src/_733/Solution.java
484485
[771-java]: ./src/_771/Solution.java
485486
[804-java]: ./src/_804/Solution.java
486487

src/_733/Solution.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _733;
2+
3+
class Solution {
4+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
5+
int oldColor = image[sr][sc];
6+
if (oldColor == newColor) {
7+
return image;
8+
}
9+
dfs(image, sr, sc, oldColor, newColor);
10+
return image;
11+
}
12+
13+
private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
14+
if (image[sr][sc] == oldColor) {
15+
image[sr][sc] = newColor;
16+
if (sr > 0) dfs(image, sr - 1, sc, oldColor, newColor);
17+
if (sr + 1 < image.length) dfs(image, sr + 1, sc, oldColor, newColor);
18+
if (sc > 0) dfs(image, sr, sc - 1, oldColor, newColor);
19+
if (sc + 1 < image[sr].length) dfs(image, sr, sc + 1, oldColor, newColor);
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
int[][] image = solution.floodFill(new int[][]{{1, 1, 1}, {1, 1, 0}, {1, 0, 1}}, 1, 1, 2);
26+
for (int[] row : image) {
27+
for (int num : row) {
28+
System.out.print(num + " ");
29+
}
30+
System.out.println();
31+
}
32+
}
33+
}

tips/733/README.md

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,71 @@
1-
[xxxx][title]
1+
[Flood Fill ][title]
22

33
## Description
4-
// 抄题目
54

5+
An `image` is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
66

7-
**Example:**
7+
Given a coordinate `(sr, sc)` representing the starting pixel (row and column) of the flood fill, and a pixel value `newColor`, "flood fill" the image.
8+
9+
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
10+
11+
At the end, return the modified image.
12+
13+
**Example 1:**
814

915
```
10-
// 抄Example
16+
Input:
17+
image = [[1,1,1],[1,1,0],[1,0,1]]
18+
sr = 1, sc = 1, newColor = 2
19+
Output: [[2,2,2],[2,2,0],[2,0,1]]
20+
Explanation:
21+
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
22+
by a path of the same color as the starting pixel are colored with the new color.
23+
Note the bottom corner is not colored 2, because it is not 4-directionally connected
24+
to the starting pixel.
1125
```
1226

1327
**Note:**
14-
// Note
1528

16-
**Tags:** // tags
29+
The length of `image` and `image[0]` will be in the range `[1, 50]`.
1730

31+
The given starting pixel will satisfy `0 <= sr < image.length` and `0 <= sc < image[0].length`.
1832

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
33+
The value of each color in `image[i][j]` and `newColor` will be an integer in `[0, 65535]`.
2334

24-
```
25-
```javascript
35+
**Tags:** [Depth-first Search](https://leetcode.com/tag/depth-first-search/)
2636

27-
```
37+
## 思路
2838

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
32-
33-
```
39+
题目给出一个二维数组来表示一张图像,数组里的值对应每个像素的值。指定一个目标像素,要求将所有和此像素连通(连通方向为上下左右四个方向)或间接连通的像素都更改为新的像素值`newColor`
3440

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
41+
简单的通过深度优先遍历将所有连通的像素改变即可。
3842

43+
```java
44+
class Solution {
45+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
46+
int oldColor = image[sr][sc];
47+
if (oldColor == newColor) {
48+
return image;
49+
}
50+
dfs(image, sr, sc, oldColor, newColor);
51+
return image;
52+
}
53+
54+
private void dfs(int[][] image, int sr, int sc, int oldColor, int newColor) {
55+
if (image[sr][sc] == oldColor) {
56+
image[sr][sc] = newColor;
57+
if (sr > 0) dfs(image, sr - 1, sc, oldColor, newColor);
58+
if (sr + 1 < image.length) dfs(image, sr + 1, sc, oldColor, newColor);
59+
if (sc > 0) dfs(image, sr, sc - 1, oldColor, newColor);
60+
if (sc + 1 < image[sr].length) dfs(image, sr, sc + 1, oldColor, newColor);
61+
}
62+
}
63+
}
3964
```
4065

4166
## 结语
42-
67+
4368
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4469

45-
[title]: https://leetcode.com/problems/xxxx
70+
[title]: https://leetcode.com/problems/flood-fill/description/
4671
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)