Skip to content

Commit 33dc936

Browse files
committed
feat: add solutions to lc problem: No.0733
No.0733.Flood Fill
1 parent df94fa5 commit 33dc936

File tree

2 files changed

+199
-1
lines changed

2 files changed

+199
-1
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ class Solution:
6262
return image
6363
```
6464

65+
```python
66+
class Solution:
67+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
68+
if image[sr][sc] == newColor:
69+
return image
70+
q = deque([(sr, sc)])
71+
oc = image[sr][sc]
72+
image[sr][sc] = newColor
73+
while q:
74+
i, j = q.popleft()
75+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
76+
x, y = i + a, j + b
77+
if 0 <= x < len(image) and 0 <= y < len(image[0]) and image[x][y] == oc:
78+
q.append((x, y))
79+
image[x][y] = newColor
80+
return image
81+
```
82+
6583
### **Java**
6684

6785
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -87,6 +105,33 @@ class Solution {
87105
}
88106
```
89107

108+
```java
109+
class Solution {
110+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
111+
if (image[sr][sc] == newColor) {
112+
return image;
113+
}
114+
Deque<int[]> q = new ArrayDeque<>();
115+
q.offer(new int[]{sr, sc});
116+
int oc = image[sr][sc];
117+
image[sr][sc] = newColor;
118+
int[] dirs = {-1, 0, 1, 0, -1};
119+
while (!q.isEmpty()) {
120+
int[] p = q.poll();
121+
int i = p[0], j = p[1];
122+
for (int k = 0; k < 4; ++k) {
123+
int x = i + dirs[k], y = j + dirs[k + 1];
124+
if (x >= 0 && x < image.length && y >= 0 && y < image[0].length && image[x][y] == oc) {
125+
q.offer(new int[]{x, y});
126+
image[x][y] = newColor;
127+
}
128+
}
129+
}
130+
return image;
131+
}
132+
}
133+
```
134+
90135
### **C++**
91136

92137
```cpp
@@ -107,6 +152,36 @@ public:
107152
};
108153
```
109154
155+
```cpp
156+
class Solution {
157+
public:
158+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
159+
if (image[sr][sc] == newColor) return image;
160+
int oc = image[sr][sc];
161+
image[sr][sc] = newColor;
162+
queue<pair<int, int>> q;
163+
q.push({sr, sc});
164+
vector<int> dirs = {-1, 0, 1, 0, -1};
165+
while (!q.empty())
166+
{
167+
auto p = q.front();
168+
q.pop();
169+
for (int k = 0; k < 4; ++k)
170+
{
171+
int x = p.first + dirs[k];
172+
int y = p.second + dirs[k + 1];
173+
if (x >= 0 && x < image.size() && y >= 0 && y < image[0].size() && image[x][y] == oc)
174+
{
175+
q.push({x, y});
176+
image[x][y] = newColor;
177+
}
178+
}
179+
}
180+
return image;
181+
}
182+
};
183+
```
184+
110185
### **Go**
111186

112187
```go
@@ -127,6 +202,30 @@ func dfs(image [][]int, i, j, oc, nc int) {
127202
}
128203
```
129204

205+
```go
206+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
207+
if image[sr][sc] == newColor {
208+
return image
209+
}
210+
oc := image[sr][sc]
211+
q := [][]int{[]int{sr, sc}}
212+
image[sr][sc] = newColor
213+
dirs := []int{-1, 0, 1, 0, -1}
214+
for len(q) > 0 {
215+
p := q[0]
216+
q = q[1:]
217+
for k := 0; k < 4; k++ {
218+
x, y := p[0]+dirs[k], p[1]+dirs[k+1]
219+
if x >= 0 && x < len(image) && y >= 0 && y < len(image[0]) && image[x][y] == oc {
220+
q = append(q, []int{x, y})
221+
image[x][y] = newColor
222+
}
223+
}
224+
}
225+
return image
226+
}
227+
```
228+
130229
### **...**
131230

132231
```

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

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ to the starting pixel.
6060

6161
## Solutions
6262

63-
DFS.
63+
DFS or BFS.
6464

6565
<!-- tabs:start -->
6666

@@ -80,6 +80,24 @@ class Solution:
8080
return image
8181
```
8282

83+
```python
84+
class Solution:
85+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
86+
if image[sr][sc] == newColor:
87+
return image
88+
q = deque([(sr, sc)])
89+
oc = image[sr][sc]
90+
image[sr][sc] = newColor
91+
while q:
92+
i, j = q.popleft()
93+
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
94+
x, y = i + a, j + b
95+
if 0 <= x < len(image) and 0 <= y < len(image[0]) and image[x][y] == oc:
96+
q.append((x, y))
97+
image[x][y] = newColor
98+
return image
99+
```
100+
83101
### **Java**
84102

85103
```java
@@ -103,6 +121,33 @@ class Solution {
103121
}
104122
```
105123

124+
```java
125+
class Solution {
126+
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
127+
if (image[sr][sc] == newColor) {
128+
return image;
129+
}
130+
Deque<int[]> q = new ArrayDeque<>();
131+
q.offer(new int[]{sr, sc});
132+
int oc = image[sr][sc];
133+
image[sr][sc] = newColor;
134+
int[] dirs = {-1, 0, 1, 0, -1};
135+
while (!q.isEmpty()) {
136+
int[] p = q.poll();
137+
int i = p[0], j = p[1];
138+
for (int k = 0; k < 4; ++k) {
139+
int x = i + dirs[k], y = j + dirs[k + 1];
140+
if (x >= 0 && x < image.length && y >= 0 && y < image[0].length && image[x][y] == oc) {
141+
q.offer(new int[]{x, y});
142+
image[x][y] = newColor;
143+
}
144+
}
145+
}
146+
return image;
147+
}
148+
}
149+
```
150+
106151
### **C++**
107152

108153
```cpp
@@ -123,6 +168,36 @@ public:
123168
};
124169
```
125170
171+
```cpp
172+
class Solution {
173+
public:
174+
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
175+
if (image[sr][sc] == newColor) return image;
176+
int oc = image[sr][sc];
177+
image[sr][sc] = newColor;
178+
queue<pair<int, int>> q;
179+
q.push({sr, sc});
180+
vector<int> dirs = {-1, 0, 1, 0, -1};
181+
while (!q.empty())
182+
{
183+
auto p = q.front();
184+
q.pop();
185+
for (int k = 0; k < 4; ++k)
186+
{
187+
int x = p.first + dirs[k];
188+
int y = p.second + dirs[k + 1];
189+
if (x >= 0 && x < image.size() && y >= 0 && y < image[0].size() && image[x][y] == oc)
190+
{
191+
q.push({x, y});
192+
image[x][y] = newColor;
193+
}
194+
}
195+
}
196+
return image;
197+
}
198+
};
199+
```
200+
126201
### **Go**
127202

128203
```go
@@ -143,6 +218,30 @@ func dfs(image [][]int, i, j, oc, nc int) {
143218
}
144219
```
145220

221+
```go
222+
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
223+
if image[sr][sc] == newColor {
224+
return image
225+
}
226+
oc := image[sr][sc]
227+
q := [][]int{[]int{sr, sc}}
228+
image[sr][sc] = newColor
229+
dirs := []int{-1, 0, 1, 0, -1}
230+
for len(q) > 0 {
231+
p := q[0]
232+
q = q[1:]
233+
for k := 0; k < 4; k++ {
234+
x, y := p[0]+dirs[k], p[1]+dirs[k+1]
235+
if x >= 0 && x < len(image) && y >= 0 && y < len(image[0]) && image[x][y] == oc {
236+
q = append(q, []int{x, y})
237+
image[x][y] = newColor
238+
}
239+
}
240+
}
241+
return image
242+
}
243+
```
244+
146245
### **...**
147246

148247
```

0 commit comments

Comments
 (0)