Guest User

Untitled

a guest
Mar 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. class Solution:
  2. """
  3. :type image: List[List[int]]
  4. :type sr: int
  5. :type sc: int
  6. :type newColor: int
  7. :rtype: List[List[int]]
  8. """
  9.  
  10. def convert(self, image, r, c, newColor, x_size, y_size):
  11. if r not in range(y_size) or c not in range(x_size):
  12. return
  13. if image[r][c] == self.init_color and (r, c) not in self.prev_seen:
  14. image[r][c] = newColor
  15. self.prev_seen.add((r, c))
  16. for x, y in ((r-1, c), (r+1, c), (r, c-1), (r, c+1)):
  17. self.convert(image, x, y, newColor, x_size, y_size)
  18.  
  19. def floodFill(self, image, sr, sc, newColor):
  20.  
  21. if not image:
  22. return
  23. y_size = len(image)
  24. x_size = len(image[0])
  25. self.init_color = image[sr][sc]
  26. self.prev_seen = set()
  27. self.convert(image, sr, sc, newColor, x_size, y_size)
  28.  
  29. return image
Add Comment
Please, Sign In to add comment