Skip to content

Commit 13e3903

Browse files
committed
LeetCode: 733 solved
1 parent ba53448 commit 13e3903

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Python/LeetCode/733. Flood Fill.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# https://leetcode.com/problems/flood-fill/solution/
2+
3+
class Solution:
4+
def floodFill(self, image: [[int]], sr: int, sc: int, newColor: int) -> [[int]]:
5+
6+
self.dfs(image, sr, sc, newColor, image[sr][sc])
7+
return image
8+
9+
def dfs(self, image, sr, sc, newColor, originalColor):
10+
if sr < 0 or sr >= len(image):
11+
return
12+
if sc < 0 or sc >= len(image[0]):
13+
return
14+
15+
if image[sr][sc] == newColor:
16+
return
17+
18+
if image[sr][sc] == originalColor:
19+
image[sr][sc] = newColor
20+
21+
self.dfs(image, sr - 1, sc, newColor, originalColor)
22+
self.dfs(image, sr + 1, sc, newColor, originalColor)
23+
self.dfs(image, sr, sc + 1, newColor, originalColor)
24+
self.dfs(image, sr, sc - 1, newColor, originalColor)

0 commit comments

Comments
 (0)