python3:
虽然是简单题 但是我还是不太会子 学习一下dfs
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
n, m = len(image), len(image[0])
curcolor = image[sr][sc]
def dfs(x: int, y: int):
if image[x][y] == curcolor:
image[x][y] = newColor
for mx, my in [(x - 1, y),(x + 1, y),(x, y - 1),(x, y + 1)]:
if 0 <= mx < n and 0 <= my < m and image[mx][my] == curcolor:
dfs(mx,my)
if curcolor != newColor:
dfs(sr,sc)
return image