Home leetCode Day5
Post
Cancel

leetCode Day5

Flood Fill

題目:

1
給一個二維陣列,每個欄位都有顏色,給一個座標需染成目標色,如果鄰近的座標顏色都相同的話,會一起被染成目標色

解法:

code

func floodFill(image [][]int, sr int, sc int, color int) [][]int {
    if image[sr][sc] == color {
        return image
    }

    oldColor := image[sr][sc]
    m := len(image)
    n := len(image[0])
    
    image[sr][sc] = color
    for _, value := range offset {
        x := sr + value[0]
        y := sc + value[1]
        if x >= 0 && x < m && y >= 0 && y < n && image[x][y] == oldColor {
            floodFill(image, x, y, color)
        }
    }
    
    return image
}
This post is licensed under CC BY 4.0 by the author.

Trending Tags