733. 影象渲染
阿新 • • 發佈:2021-12-17
有一幅以二維整數陣列表示的圖畫,每一個整數表示該圖畫的畫素值大小,數值在 0 到 65535 之間。
給你一個座標(sr, sc)表示影象渲染開始的畫素值(行 ,列)和一個新的顏色值newColor,讓你重新上色這幅影象。
為了完成上色工作,從初始座標開始,記錄初始座標的上下左右四個方向上畫素值與初始座標相同的相連畫素點,接著再記錄這四個方向上符合條件的畫素點與他們對應四個方向上畫素值與初始座標相同的相連畫素點,……,重複該過程。將所有有記錄的畫素點的顏色值改為新的顏色值。
最後返回經過上色渲染後的影象。
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/flood-fill
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。
心之所向,素履以往 生如逆旅,一葦以航import java.util.LinkedList; class Solution { private int[] dx = {0, 1, 0, -1}; private int[] dy = {1, 0, -1, 0}; public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { int n = image.length, m = image[0].length; int color = image[sr][sc]; if (color == newColor) { return image; } LinkedList<int[]> queue = new LinkedList<>(); queue.offer(new int[]{sr, sc}); image[sr][sc] = newColor; while (!queue.isEmpty()) { int[] node = queue.poll(); for (int i = 0; i < 4; ++i) { int x = node[0] + dx[i]; int y = node[1] + dy[i]; if (x >= 0 && x < n && y >= 0 && y < m && image[x][y] == color) { queue.offer(new int[]{x, y}); image[x][y] = newColor; } } } return image; } }