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); } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/72216.html
摘要:原文鏈接最近在知乎上看到一個問題,隨機(jī)生成指定面積單連通區(qū)域,感覺還挺有意思的,于是整理一下寫一篇新文章。問題闡述如下圖所示,在的區(qū)域中,隨機(jī)生成面積為的單連通區(qū)域,該隨機(jī)包括位置隨機(jī)以及形狀隨機(jī)。 原文鏈接:https://xcoder.in/2018/04/01/random-connected-area/ 最近在知乎上看到一個問題,「隨機(jī)生成指定面積單連通區(qū)域?」,感覺還挺有意...
摘要:作為智能全球防御體系產(chǎn)品矩陣之一的全球清洗,針對云內(nèi)資源提供防御,防護(hù)覆蓋東南亞所有地區(qū),延時低至。亞太高防部署簡單,購買后只需將高防綁定到需要防護(hù)的云產(chǎn)品上即可生效。具有實時防御低延時高可靠大防護(hù)的特點。支持彈性防護(hù)容量最大可至后付費。作為UCloud智能全球DDoS防御體系產(chǎn)品矩陣之一的全球清洗UanycastClean,針對UCloud云內(nèi)資源提供DDoS防御,防護(hù)覆蓋東南亞所有地區(qū),...
摘要:空間復(fù)雜度方法是否為最大的冪的約數(shù)思路最大的的冪為,判斷是否是的約數(shù)即可。復(fù)雜度時間復(fù)雜度,一個整數(shù)統(tǒng)計二進(jìn)制的復(fù)雜度,最壞的情況下是。 大廠算法面試之leetcode精講9.位運(yùn)算視頻教程(高效學(xué)習(xí)):點擊學(xué)習(xí)目錄:1.開篇介紹2.時間空間復(fù)雜度3.動態(tài)規(guī)劃4.貪心5.二分查找6.深度優(yōu)先&廣度優(yōu)先7.雙指針...
閱讀 3413·2021-10-11 11:06
閱讀 2194·2019-08-29 11:10
閱讀 1956·2019-08-26 18:18
閱讀 3262·2019-08-26 13:34
閱讀 1568·2019-08-23 16:45
閱讀 1046·2019-08-23 16:29
閱讀 2807·2019-08-23 13:11
閱讀 3237·2019-08-23 12:58