Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
- def bfs(x,y):
- q = deque()
- q.append((x,y))
- oldcolor = image[x][y]
- image[x][y] = newColor
- if newColor == oldcolor:
- return
- while len(q) > 0:
- (r,c) = q.popleft()
- for row,col in neighbors(r,c):
- if image[row][col] == oldcolor:
- q.append((row,col))
- image[row][col]= newColor
- def neighbors(row,col):
- result=[]
- if row-1 >= 0:
- result.append((row-1,col))
- if col-1 >=0:
- result.append((row,col-1))
- if row+1 < len(image):
- result.append((row+1,col))
- if col+1 < len(image[0]):
- result.append((row,col+1))
- return result
- bfs(sr,sc)
- return image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement