Problem
An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
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 as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
At the end, return the modified image.
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)), all pixels connected
by a path of the same color as the starting pixel 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.
Note:
The length of image and image[0] will be in the range [1, 50].
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
The value of each color in imagei and newColor will be an integer in [0, 65535].
class Solution { public int[][] floodFill(int[][] image, int sr, int sc, int newColor) { if (image[sr][sc] == newColor) return image; dfs(image, sr, sc, image[sr][sc], newColor); return image; } public void dfs(int[][] image, int i, int j, int pre, int cur) { if (i >= 0 && i < image.length && j >= 0 && j < image[0].length && image[i][j] == pre) { image[i][j] = cur; dfs(image, i-1, j, pre, cur); dfs(image, i+1, j, pre, cur); dfs(image, i, j-1, pre, cur); dfs(image, i, j+1, pre, cur); } } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72216.html
摘要:原文鏈接最近在知乎上看到一個問題,隨機生成指定面積單連通區域,感覺還挺有意思的,于是整理一下寫一篇新文章。問題闡述如下圖所示,在的區域中,隨機生成面積為的單連通區域,該隨機包括位置隨機以及形狀隨機。 原文鏈接:https://xcoder.in/2018/04/01/random-connected-area/ 最近在知乎上看到一個問題,「隨機生成指定面積單連通區域?」,感覺還挺有意...
摘要:作為智能全球防御體系產品矩陣之一的全球清洗,針對云內資源提供防御,防護覆蓋東南亞所有地區,延時低至。亞太高防部署簡單,購買后只需將高防綁定到需要防護的云產品上即可生效。具有實時防御低延時高可靠大防護的特點。支持彈性防護容量最大可至后付費。作為UCloud智能全球DDoS防御體系產品矩陣之一的全球清洗UanycastClean,針對UCloud云內資源提供DDoS防御,防護覆蓋東南亞所有地區,...
摘要:空間復雜度方法是否為最大的冪的約數思路最大的的冪為,判斷是否是的約數即可。復雜度時間復雜度,一個整數統計二進制的復雜度,最壞的情況下是。 大廠算法面試之leetcode精講9.位運算視頻教程(高效學習):點擊學習目錄:1.開篇介紹2.時間空間復雜度3.動態規劃4.貪心5.二分查找6.深度優先&廣度優先7.雙指針...
閱讀 3398·2021-10-11 11:06
閱讀 2182·2019-08-29 11:10
閱讀 1944·2019-08-26 18:18
閱讀 3255·2019-08-26 13:34
閱讀 1559·2019-08-23 16:45
閱讀 1037·2019-08-23 16:29
閱讀 2797·2019-08-23 13:11
閱讀 3226·2019-08-23 12:58