国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 733. Flood Fill

church / 1212人閱讀

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);
        }
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72216.html

相關文章

  • 隨機生成指定面積單連通區域

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

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

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

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

    摘要:空間復雜度方法是否為最大的冪的約數思路最大的的冪為,判斷是否是的約數即可。復雜度時間復雜度,一個整數統計二進制的復雜度,最壞的情況下是。 大廠算法面試之leetcode精講9.位運算視頻教程(高效學習):點擊學習目錄:1.開篇介紹2.時間空間復雜度3.動態規劃4.貪心5.二分查找6.深度優先&廣度優先7.雙指針...

    番茄西紅柿 評論0 收藏2637

發表評論

0條評論

church

|高級講師

TA的文章

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