Leetcode Note: Go - Flood Fill

Flood Fill - LeetCode
https://leetcode.com/problems/flood-fill/

  • Go 言語で取り組んだメモ

所感

  • image の座標を操作する

回答

Flood Fill - LeetCode
https://leetcode.com/problems/flood-fill/solution/

Go clean code - LeetCode Discuss
https://leetcode.com/problems/flood-fill/discuss/1529928/Go-clean-code

func floodFill(image [][]int, sr int, sc int, newColor int) [][]int {
    row,coloumn := len(image) , len(image[0])
    
    var dfs func(r,c int)
    
    color := image[sr][sc]
    
    if color == newColor {
        return image
    }
    
    dfs = func(r,c int){
        if image[r][c] == color{
            image[r][c] = newColor
            if r >= 1 {
                dfs(r-1,c)
            }
            if r+1 < row {
                dfs(r+1,c)
            }
            if c>= 1{
                dfs(r,c-1)
            }
            if c+1 < coloumn{
                dfs(r,c+1)
            }
        }
    }
    dfs(sr,sc)
    return image
}