733. 图像渲染
有一幅以 m x n
的二维整数数组表示的图画 image
,其中 image[i][j]
表示该图画的像素值大小。
你也被给予三个整数 sr
, sc
和 newColor
。你应该从像素 image[sr][sc]
开始对图像进行 上色填充 。
为了完成 上色工作 ,从初始像素开始,记录初始坐标的 上下左右四个方向上 像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应 四个方向上 像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为 newColor
。
最后返回 经过上色渲染后的图像 。
示例 1:
输入: 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
相似题目
原站题解
golang 解法, 执行用时: 4 ms, 内存消耗: 3.6 MB, 提交时间: 2023-11-17 17:44:43
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 }
golang 解法, 执行用时: 4 ms, 内存消耗: 3.5 MB, 提交时间: 2023-11-17 17:44:30
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) } } } }
python3 解法, 执行用时: 56 ms, 内存消耗: 16.3 MB, 提交时间: 2023-11-17 17:44:12
class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: currColor = image[sr][sc] if currColor == color: return image n, m = len(image), len(image[0]) que = collections.deque([(sr, sc)]) image[sr][sc] = color while que: x, y = que.popleft() for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if 0 <= mx < n and 0 <= my < m and image[mx][my] == currColor: que.append((mx, my)) image[mx][my] = color return image def floodFill2(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: n, m = len(image), len(image[0]) currColor = image[sr][sc] def dfs(x: int, y: int): if image[x][y] == currColor: image[x][y] = color for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if 0 <= mx < n and 0 <= my < m and image[mx][my] == currColor: dfs(mx, my) if currColor != color: dfs(sr, sc) return image
java 解法, 执行用时: 0 ms, 内存消耗: 42.9 MB, 提交时间: 2023-11-17 17:43:39
class Solution { int[] dx = {1, 0, 0, -1}; int[] dy = {0, 1, -1, 0}; public int[][] floodFill(int[][] image, int sr, int sc, int color) { int currColor = image[sr][sc]; if (currColor != color) { dfs(image, sr, sc, currColor, color); } return image; } public void dfs(int[][] image, int x, int y, int currColor, int color) { if (image[x][y] == currColor) { image[x][y] = color; for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < image.length && my >= 0 && my < image[0].length) { dfs(image, mx, my, currColor, color); } } } } } class Solution2 { int[] dx = {1, 0, 0, -1}; int[] dy = {0, 1, -1, 0}; public int[][] floodFill(int[][] image, int sr, int sc, int color) { int currColor = image[sr][sc]; if (currColor == color) { return image; } int n = image.length, m = image[0].length; Queue<int[]> queue = new LinkedList<int[]>(); queue.offer(new int[]{sr, sc}); image[sr][sc] = color; while (!queue.isEmpty()) { int[] cell = queue.poll(); int x = cell[0], y = cell[1]; for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) { queue.offer(new int[]{mx, my}); image[mx][my] = color; } } } return image; } }
cpp 解法, 执行用时: 8 ms, 内存消耗: 14.3 MB, 提交时间: 2023-11-17 17:43:12
// bfs class Solution { public: const int dx[4] = {1, 0, 0, -1}; const int dy[4] = {0, 1, -1, 0}; vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) { int currColor = image[sr][sc]; if (currColor == color) { return image; } int n = image.size(), m = image[0].size(); queue<pair<int, int>> que; que.emplace(sr, sc); image[sr][sc] = color; while (!que.empty()) { int x = que.front().first, y = que.front().second; que.pop(); for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) { que.emplace(mx, my); image[mx][my] = color; } } } return image; } }; // dfs class Solution2 { public: const int dx[4] = {1, 0, 0, -1}; const int dy[4] = {0, 1, -1, 0}; void dfs(vector<vector<int>>& image, int x, int y, int currColor, int color) { if (image[x][y] == currColor) { image[x][y] = color; for (int i = 0; i < 4; i++) { int mx = x + dx[i], my = y + dy[i]; if (mx >= 0 && mx < image.size() && my >= 0 && my < image[0].size()) { dfs(image, mx, my, currColor, color); } } } } vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) { int currColor = image[sr][sc]; if (currColor != color) { dfs(image, sr, sc, currColor, color); } return image; } };
golang 解法, 执行用时: 4 ms, 内存消耗: 4 MB, 提交时间: 2022-07-19 16:36:54
func floodFill(image [][]int, sr int, sc int, newColor int) [][]int { helper(image, sr, sc, image[sr][sc], newColor) return image } func helper(image [][]int, i, j int, oldColor, newColor int) { if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oldColor || image[i][j] == newColor { return } image[i][j] = newColor helper(image, i+1, j, oldColor, newColor) helper(image, i-1, j, oldColor, newColor) helper(image, i, j+1, oldColor, newColor) helper(image, i, j-1, oldColor, newColor) }