Skip to content

Commit 6d2d051

Browse files
committed
feat: add solutions to lc problem: No.0733
No.0733.Flood Fill
1 parent d631bfb commit 6d2d051

File tree

6 files changed

+200
-6
lines changed

6 files changed

+200
-6
lines changed

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,95 @@ sr = 1, sc = 1, newColor = 2
3636
<li><code>image[i][j]</code> 和&nbsp;<code>newColor</code>&nbsp;表示的颜色值在范围&nbsp;<code>[0, 65535]</code>内。</li>
3737
</ul>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
4342

43+
DFS。
44+
4445
<!-- tabs:start -->
4546

4647
### **Python3**
4748

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

5051
```python
51-
52+
class Solution:
53+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
54+
def dfs(i, j, oc, nc):
55+
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
56+
return
57+
image[i][j] = nc
58+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
59+
dfs(i + x, j + y, oc, nc)
60+
61+
dfs(sr, sc, image[sr][sc], newColor)
62+
return image
5263
```
5364

5465
### **Java**
5566

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

5869
```java
70+
class Solution {
71+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
72+
73+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
74+
dfs(image, sr, sc, image[sr][sc], newColor);
75+
return image;
76+
}
77+
78+
private void dfs(int[][] image, int i, int j, int oc, int nc) {
79+
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
80+
return;
81+
}
82+
image[i][j] = nc;
83+
for (int[] dir : dirs) {
84+
dfs(image, i + dir[0], j + dir[1], oc, nc);
85+
}
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
96+
97+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
98+
dfs(image, sr, sc, image[sr][sc], newColor);
99+
return image;
100+
}
101+
102+
void dfs(vector<vector<int>>& image, int i, int j, int oc, int nc) {
103+
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != oc || image[i][j] == nc) return;
104+
image[i][j] = nc;
105+
for (auto& dir : dirs) dfs(image, i + dir[0], j + dir[1], oc, nc);
106+
}
107+
};
108+
```
59109
110+
### **Go**
111+
112+
```go
113+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
114+
dfs(image, sr, sc, image[sr][sc], newColor)
115+
return image
116+
}
117+
118+
func dfs(image [][]int, i, j, oc, nc int) {
119+
if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
120+
return
121+
}
122+
image[i][j] = nc
123+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
124+
for _, dir := range dirs {
125+
dfs(image, i+dir[0], j+dir[1], oc, nc)
126+
}
127+
}
60128
```
61129

62130
### **...**

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

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Given a coordinate <code>(sr, sc)</code> representing the starting pixel (row an
1414

1515
</p><p>
1616

17-
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.
17+
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.
1818

1919
</p><p>
2020

@@ -48,8 +48,6 @@ to the starting pixel.
4848

4949
</p>
5050

51-
52-
5351
<p><b>Note:</b>
5452

5553
<li>The length of <code>image</code> and <code>image[0]</code> will be in the range <code>[1, 50]</code>.</li>
@@ -62,18 +60,87 @@ to the starting pixel.
6260

6361
## Solutions
6462

63+
DFS.
64+
6565
<!-- tabs:start -->
6666

6767
### **Python3**
6868

6969
```python
70-
70+
class Solution:
71+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
72+
def dfs(i, j, oc, nc):
73+
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
74+
return
75+
image[i][j] = nc
76+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
77+
dfs(i + x, j + y, oc, nc)
78+
79+
dfs(sr, sc, image[sr][sc], newColor)
80+
return image
7181
```
7282

7383
### **Java**
7484

7585
```java
86+
class Solution {
87+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
88+
89+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
90+
dfs(image, sr, sc, image[sr][sc], newColor);
91+
return image;
92+
}
93+
94+
private void dfs(int[][] image, int i, int j, int oc, int nc) {
95+
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
96+
return;
97+
}
98+
image[i][j] = nc;
99+
for (int[] dir : dirs) {
100+
dfs(image, i + dir[0], j + dir[1], oc, nc);
101+
}
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
112+
113+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
114+
dfs(image, sr, sc, image[sr][sc], newColor);
115+
return image;
116+
}
117+
118+
void dfs(vector<vector<int>>& image, int i, int j, int oc, int nc) {
119+
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != oc || image[i][j] == nc) return;
120+
image[i][j] = nc;
121+
for (auto& dir : dirs) dfs(image, i + dir[0], j + dir[1], oc, nc);
122+
}
123+
};
124+
```
76125
126+
### **Go**
127+
128+
```go
129+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
130+
dfs(image, sr, sc, image[sr][sc], newColor)
131+
return image
132+
}
133+
134+
func dfs(image [][]int, i, j, oc, nc int) {
135+
if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
136+
return
137+
}
138+
image[i][j] = nc
139+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
140+
for _, dir := range dirs {
141+
dfs(image, i+dir[0], j+dir[1], oc, nc)
142+
}
143+
}
77144
```
78145

79146
### **...**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
4+
5+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
6+
dfs(image, sr, sc, image[sr][sc], newColor);
7+
return image;
8+
}
9+
10+
void dfs(vector<vector<int>>& image, int i, int j, int oc, int nc) {
11+
if (i < 0 || i >= image.size() || j < 0 || j >= image[0].size() || image[i][j] != oc || image[i][j] == nc) return;
12+
image[i][j] = nc;
13+
for (auto& dir : dirs) dfs(image, i + dir[0], j + dir[1], oc, nc);
14+
}
15+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
2+
dfs(image, sr, sc, image[sr][sc], newColor)
3+
return image
4+
}
5+
6+
func dfs(image [][]int, i, j, oc, nc int) {
7+
if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
8+
return
9+
}
10+
image[i][j] = nc
11+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
12+
for _, dir := range dirs {
13+
dfs(image, i+dir[0], j+dir[1], oc, nc)
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
private int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
3+
4+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
5+
dfs(image, sr, sc, image[sr][sc], newColor);
6+
return image;
7+
}
8+
9+
private void dfs(int[][] image, int i, int j, int oc, int nc) {
10+
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc || image[i][j] == nc) {
11+
return;
12+
}
13+
image[i][j] = nc;
14+
for (int[] dir : dirs) {
15+
dfs(image, i + dir[0], j + dir[1], oc, nc);
16+
}
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
3+
def dfs(i, j, oc, nc):
4+
if i < 0 or i >= len(image) or j < 0 or j >= len(image[0]) or image[i][j] != oc or image[i][j] == nc:
5+
return
6+
image[i][j] = nc
7+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
8+
dfs(i + x, j + y, oc, nc)
9+
10+
dfs(sr, sc, image[sr][sc], newColor)
11+
return image

0 commit comments

Comments
 (0)