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 newColor
. You should perform a flood fill on the image starting from the pixel image[sr][sc]
.
To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with newColor
.
Return the modified image after performing the flood fill.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2 Output: [[2,2,2],[2,2,0],[2,0,1]] Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color. Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
Example 2:
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2 Output: [[2,2,2],[2,2,2]]
Constraints:
m == image.length
n == image[i].length
1 <= m, n <= 50
0 <= image[i][j], newColor < 216
0 <= sr < m
0 <= sc < n
图像渲染。
有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间。
给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newColor,让你重新上色这幅图像。
为了完成上色工作,从初始坐标开始,记录初始坐标的上下左右四个方向上像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应四个方向上像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为新的颜色值。
最后返回经过上色渲染后的图像。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/flood-fill
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这一题跟之前的695和547很类似。题目给了起始位置和需要更新的颜色 newColor,思路是用 DFS 判断四个方向的坐标上是否需要被更新颜色。
注意如果
- 超出矩阵范围
- 或者颜色跟初始坐标上的颜色(image[sr][sc])不同
- 又或者已经被染色过了(已经是newColor)
则直接 return。同时如果一开始给的坐标也是被染色过了的(newColor),就直接退出了,无需任何操作。因为染色的动作一定是要从当前坐标开始被触发的。
时间O(mn)
空间O(mn)
Java实现
1 class Solution { 2 public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { 3 // corner case 4 if (image[sr][sc] == newColor) { 5 return image; 6 } 7 8 // normal case 9 helper(image, sr, sc, image[sr][sc], newColor); 10 return image; 11 } 12 13 private void helper(int[][] image, int sr, int sc, int color, int newColor) { 14 if (sr < 0 || sr >= image.length || sc < 0 || sc >= image[0].length || image[sr][sc] != color) { 15 return; 16 } 17 image[sr][sc] = newColor; 18 helper(image, sr + 1, sc, color, newColor); 19 helper(image, sr - 1, sc, color, newColor); 20 helper(image, sr, sc + 1, color, newColor); 21 helper(image, sr, sc - 1, color, newColor); 22 } 23 }
二刷我也试着用 BFS 做了一下,时间空间复杂度同 DFS。
Java实现
1 class Solution { 2 int[] dx = { 1, 0, 0, -1 }; 3 int[] dy = { 0, 1, -1, 0 }; 4 5 public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { 6 int curColor = image[sr][sc]; 7 // corner case 8 if (curColor == newColor) { 9 return image; 10 } 11 12 // normal case 13 int m = image.length; 14 int n = image[0].length; 15 Queue<int[]> queue = new LinkedList<>(); 16 queue.offer(new int[] { sr, sc }); 17 image[sr][sc] = newColor; 18 while (!queue.isEmpty()) { 19 int[] cur = queue.poll(); 20 int x = cur[0]; 21 int y = cur[1]; 22 for (int i = 0; i < 4; i++) { 23 int mx = x + dx[i]; 24 int my = y + dy[i]; 25 if (mx >= 0 && mx < m && my >= 0 && my < n && image[mx][my] == curColor) { 26 queue.offer(new int[] { mx, my }); 27 image[mx][my] = newColor; 28 } 29 } 30 } 31 return image; 32 } 33 }