成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 733. Flood Fill

church / 1219人閱讀

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].

Solution
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

相關(guān)文章

  • 隨機(jī)生成指定面積單連通區(qū)域

    摘要:原文鏈接最近在知乎上看到一個問題,隨機(jī)生成指定面積單連通區(qū)域,感覺還挺有意思的,于是整理一下寫一篇新文章。問題闡述如下圖所示,在的區(qū)域中,隨機(jī)生成面積為的單連通區(qū)域,該隨機(jī)包括位置隨機(jī)以及形狀隨機(jī)。 原文鏈接:https://xcoder.in/2018/04/01/random-connected-area/ 最近在知乎上看到一個問題,「隨機(jī)生成指定面積單連通區(qū)域?」,感覺還挺有意...

    zhoutk 評論0 收藏0
  • UCloud智能全球DDoS防御體系:亞太高防UADS-APAC-部署簡單,實時防御、低延時、高可靠

    摘要:作為智能全球防御體系產(chǎn)品矩陣之一的全球清洗,針對云內(nèi)資源提供防御,防護(hù)覆蓋東南亞所有地區(qū),延時低至。亞太高防部署簡單,購買后只需將高防綁定到需要防護(hù)的云產(chǎn)品上即可生效。具有實時防御低延時高可靠大防護(hù)的特點。支持彈性防護(hù)容量最大可至后付費。作為UCloud智能全球DDoS防御體系產(chǎn)品矩陣之一的全球清洗UanycastClean,針對UCloud云內(nèi)資源提供DDoS防御,防護(hù)覆蓋東南亞所有地區(qū),...

    codercao 評論0 收藏0
  • 大廠算法面試之leetcode精講9.位運(yùn)算

    摘要:空間復(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.雙指針...

    番茄西紅柿 評論0 收藏2637

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<