smj007

Untitled

Jun 21st, 2022
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. class Solution:
  2.     def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
  3.        
  4.         newColor = color
  5.         prevColor = image[sr][sc]
  6.        
  7.         def fill(r, c):
  8.             if r < 0 or c < 0 or r > len(image) - 1 or c > len(image[0]) - 1:
  9.                 return
  10.            
  11.             # check if already visited
  12.             if image[r][c] == newColor:
  13.                 return
  14.            
  15.             # check if the cell is cock-blocked:
  16.             if image[r][c] == prevColor:
  17.                 image[r][c] = newColor
  18.             else:
  19.                 return
  20.            
  21.             fill(r - 1, c)
  22.             fill(r, c - 1)
  23.             fill(r + 1, c)
  24.             fill(r, c + 1)
  25.  
  26.         fill(sr, sc)
  27.        
  28.         return image
Add Comment
Please, Sign In to add comment