LeetCode 75 —— 733. 图像渲染

104 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第17天,点击查看活动详情

LeetCode 75 —— 733. 图像渲染

一、题目描述:

有一幅以 m x n 的二维整数数组表示的图画 image ,其中 image[i][j] 表示该图画的像素值大小。

你也被给予三个整数 sr , sc 和 newColor 。你应该从像素 image[sr][sc] 开始对图像进行 上色填充 。

为了完成 上色工作 ,从初始像素开始,记录初始坐标的 上下左右四个方向上 像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应 四个方向上 像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为 newColor 。

最后返回 经过上色渲染后的图像 。

示例 1:

image.png

输入: image = [[1,1,1],[1,1,0],[1,0,1]],sr = 1, sc = 1, newColor = 2

输出: [[2,2,2],[2,2,0],[2,0,1]]

解析: 在图像的正中间,(坐标(sr,sc)=(1,1)),在路径上所有符合条件的像素点的颜色都被更改成2。 注意,右下角的像素没有更改为2,因为它不是在上下左右四个方向上与初始点相连的像素点。

示例 2:

输入: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2

输出: [[2,2,2],[2,2,2]]

提示:

m == image.length

n == image[i].length

1 <= m, n <= 50

0 <= image[i][j], newColor < 216

0 <= sr < m

0 <= sc < n

来源:力扣(LeetCode)

链接:leetcode.cn/problems/fl…

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

二、思路分析:

  1. 这道题考察了什么思想?你的思路是什么?

    对于这种岛屿问题的DFS,我们使用简单明了递归即可,递归的退出条件如下:

    • 行号小于0
    • 列号小于0
    • 行号超过image数组的行数
    • 列号超过image数组的列数
    • 当前上色渲染的像素块 不等于 初始的oldColor
    • newColor 等于 oldColor

    然后将image像素块赋为newColor。

    然后就进入递归:

    从当前像素块去往上下左右即可。

  2. 做题的时候是不是一次通过的,遇到了什么问题,需要注意什么细节?

    不是一次通过的,刚开始递归的退出条件设计得不太好,有几个条件没有考虑好,比如:

    • sr > len(image)-1 || sc > len(image[0])-1 使用的是sr > len(image) || sc > len(image[0])
    • 当前上色渲染的像素块 不等于 初始的oldColor
    • newColor 等于 oldColor
  3. 有几种解法,哪种解法时间复杂度最低,哪种解法空间复杂度最低,最优解法是什么?其他人的题解是什么,谁的效率更好一些?用不同语言实现的话,哪个语言速度最快?

image.png

   var (
       dx = []int{1, 0, 0, -1}
       dy = []int{0, 1, -1, 0}
   )
   ​
   func floodFill(image [][]int, sr int, sc int, color int) [][]int {
       currColor := image[sr][sc]
       if currColor == color {
           return image
       }
       n, m := len(image), len(image[0])
       queue := [][]int{}
       queue = append(queue, []int{sr, sc})
       image[sr][sc] = color
       for i := 0; i < len(queue); i++ {
           cell := queue[i]
           for j := 0; j < 4; j++ {
               mx, my := cell[0] + dx[j], cell[1] + dy[j]
               if mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor {
                   queue = append(queue, []int{mx, my})
                   image[mx][my] = color
               }
           }
       }
       return image
   }
   ​
   作者:LeetCode-Solution
   链接:https://leetcode.cn/problems/flood-fill/solution/tu-xiang-xuan-ran-by-leetcode-solution/
   来源:力扣(LeetCode)
   著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

image.png

    var (
        dx = []int{1, 0, 0, -1}
        dy = []int{0, 1, -1, 0}
    )
    ​
    func floodFill(image [][]int, sr int, sc int, color int) [][]int {
        currColor := image[sr][sc]
        if currColor != color {
            dfs(image, sr, sc, currColor, color)
        }
        return image
    }
    ​
    func dfs(image [][]int, x, y, currColor, color int) {
        if image[x][y] == currColor {
            image[x][y] = color
            for i := 0; i < 4; i++ {
                mx, my := x + dx[i], y + dy[i]
                if mx >= 0 && mx < len(image) && my >= 0 && my < len(image[0]) {
                    dfs(image, mx, my, currColor, color)
                }
            }
        }
    }
    ​
    作者:LeetCode-Solution
    链接:https://leetcode.cn/problems/flood-fill/solution/tu-xiang-xuan-ran-by-leetcode-solution/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

三、AC 代码:

func floodFill(image [][]int, sr int, sc int, color int) [][]int {
    helper(image,sr,sc,color,image[sr][sc])
    return image
}
​
func helper(image [][]int,sr, sc, newColor, oldColor int) {
    if sr < 0 || sc < 0 || sr > len(image)-1 || sc > len(image[0])-1 || image[sr][sc] != oldColor || newColor == oldColor{
         return 
     }
​
    image[sr][sc] = newColor
    helper(image,sr+1,sc,newColor,oldColor)
    helper(image,sr,sc+1,newColor,oldColor)
    helper(image,sr-1,sc,newColor,oldColor)
    helper(image,sr,sc-1,newColor,oldColor)
}
​

四、总结:

广度优先搜索的时间复杂度和空间复杂度为O(n*m),深度优先搜索的时间复杂度和空间复杂度为O(n*m)。

这种类似的岛屿问题,我们都可以用上述的AC代码模板来做。

模板来源

作者:掘金酱

链接:juejin.cn/post/706970…

来源:稀土掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。