Skip to content

Commit 15a6493

Browse files
committed
Flood Fill
1 parent 7f65fce commit 15a6493

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

733-flood-fill.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@
2929
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].
3030
"""
3131
class Solution:
32+
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
33+
34+
def dfs(x, y):
35+
if x >= 0 and x < len(image) and y >= 0 and y < len(image[0]) and image[x][y] == color:
36+
image[x][y] = newColor
37+
38+
dfs(x-1, y)
39+
dfs(x, y-1)
40+
dfs(x+1, y)
41+
dfs(x, y+1)
42+
43+
color = image[sr][sc]
44+
if color != newColor:
45+
dfs(sr, sc)
46+
return image
47+
48+
49+
50+
class Solution1:
3251
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
3352
m, n = len(image), len(image[0])
3453
color = image[sr][sc]

0 commit comments

Comments
 (0)