Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 735 Bytes

733.md

File metadata and controls

31 lines (22 loc) · 735 Bytes

Flood Fill

Description

link


Solution

  • See Code

Code

最坏情况:O(n^2)

class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
        rows, cols, orig_color = len(image), len(image[0]), image[sr][sc]
        def traverse(row, col):
            if (not (0 <= row < rows and 0 <= col < cols)) or image[row][col] != orig_color:
                return
            image[row][col] = newColor
            [traverse(row + x, col + y) for (x, y) in ((0, 1), (1, 0), (0, -1), (-1, 0))]
        if orig_color != newColor:
            traverse(sr, sc)
        return image