https://leetcode.com/problems/flood-fill/description/

 

Flood Fill - LeetCode

Flood Fill - An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[s

leetcode.com

 

EASY인데 엣지케이스땜에 생각보다 tricky했다.

내코드는 지저분해서 다른분 코드를 가져왔다.

 

class Solution:
    def floodFill(self, image, sr, sc, newColor):
        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

이분은 그냥 엣지케이스를 따로 처리해주셨다.

+ Recent posts