Skip to content

Commit 6baeda7

Browse files
committed
feat: update solutions to lc problem: No.0733
No.0733.Flood Fill
1 parent 1fcfaf0 commit 6baeda7

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

solution/0700-0799/0733.Flood Fill/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@ sr = 1, sc = 1, newColor = 2
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43-
DFS。
43+
Flood fill 算法。
44+
45+
Flood fill 算法是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法。因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名。
46+
47+
最简单的实现方法是采用 DFS 的递归方法,也可以采用 BFS 的迭代来实现。
4448

4549
<!-- tabs:start -->
4650

4751
### **Python3**
4852

4953
<!-- 这里可写当前语言的特殊实现逻辑 -->
5054

55+
DFS:
56+
5157
```python
5258
class Solution:
5359
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
@@ -62,6 +68,8 @@ class Solution:
6268
return image
6369
```
6470

71+
BFS:
72+
6573
```python
6674
class Solution:
6775
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
@@ -84,6 +92,8 @@ class Solution:
8492

8593
<!-- 这里可写当前语言的特殊实现逻辑 -->
8694

95+
DFS:
96+
8797
```java
8898
class Solution {
8999
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
@@ -105,6 +115,8 @@ class Solution {
105115
}
106116
```
107117

118+
BFS:
119+
108120
```java
109121
class Solution {
110122
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
@@ -134,6 +146,8 @@ class Solution {
134146

135147
### **C++**
136148

149+
DFS:
150+
137151
```cpp
138152
class Solution {
139153
public:
@@ -152,6 +166,8 @@ public:
152166
};
153167
```
154168
169+
BFS:
170+
155171
```cpp
156172
class Solution {
157173
public:
@@ -184,6 +200,8 @@ public:
184200

185201
### **Go**
186202

203+
DFS:
204+
187205
```go
188206
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
189207
dfs(image, sr, sc, image[sr][sc], newColor)
@@ -202,6 +220,8 @@ func dfs(image [][]int, i, j, oc, nc int) {
202220
}
203221
```
204222

223+
BFS:
224+
205225
```go
206226
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
207227
if image[sr][sc] == newColor {

solution/0700-0799/0733.Flood Fill/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ to the starting pixel.
6262

6363
DFS or BFS.
6464

65+
> Flood fill, also called seed fill, is a flooding algorithm that determines and alters the area connected to a given node in a multi-dimensional array with some matching attribute. It is used in the "bucket" fill tool of paint programs to fill connected, similarly-colored areas with a different color.
66+
6567
<!-- tabs:start -->
6668

6769
### **Python3**
6870

71+
DFS:
72+
6973
```python
7074
class Solution:
7175
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
@@ -80,6 +84,8 @@ class Solution:
8084
return image
8185
```
8286

87+
BFS:
88+
8389
```python
8490
class Solution:
8591
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
@@ -100,6 +106,8 @@ class Solution:
100106

101107
### **Java**
102108

109+
DFS:
110+
103111
```java
104112
class Solution {
105113
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
@@ -121,6 +129,8 @@ class Solution {
121129
}
122130
```
123131

132+
BFS:
133+
124134
```java
125135
class Solution {
126136
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
@@ -150,6 +160,8 @@ class Solution {
150160

151161
### **C++**
152162

163+
DFS:
164+
153165
```cpp
154166
class Solution {
155167
public:
@@ -168,6 +180,8 @@ public:
168180
};
169181
```
170182
183+
BFS:
184+
171185
```cpp
172186
class Solution {
173187
public:
@@ -200,6 +214,8 @@ public:
200214

201215
### **Go**
202216

217+
DFS:
218+
203219
```go
204220
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
205221
dfs(image, sr, sc, image[sr][sc], newColor)
@@ -218,6 +234,8 @@ func dfs(image [][]int, i, j, oc, nc int) {
218234
}
219235
```
220236

237+
BFS:
238+
221239
```go
222240
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
223241
if image[sr][sc] == newColor {

solution/0700-0799/0733.Flood Fill/Solution.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function floodFill(
77
const m = image.length;
88
const n = image[0].length;
99
const target = image[sr][sc];
10-
// 递归函数
1110
const dfs = (i: number, j: number) => {
1211
if (
1312
i < 0 ||

0 commit comments

Comments
 (0)