733. Flood Fill Simple Dyeing Problem

[copy title]:

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.

 

 

 [brute force solution]:

Time analysis:

Space Analysis:

 [Optimized]:

Time analysis:

Space Analysis:

[Wonderful output conditions]:

[Wonderful corner case]:

[Thinking question]:

Forgot the template format of dfs, thinking that the exit conditions should be written separately. But in fact the exit conditions are written together in advance.

[One sentence idea]:

[Input amount]: Empty: Normal situation: Extra large: Extra small: Special situations handled in the program: Abnormal situations (Illegal and unreasonable input):

[Paint]:

[One brush]:

[Second brush]:

[Three brushes]:

[Four brushes]:

[Five brushes]:

  [Results of five-minute naked eye debug]:

[Summarize]:

[Complexity]: Time complexity: O( ) Space complexity: O( )

[English data structures or algorithms, why not use other data structures or algorithms]:

[Key templating code]:

[Other solutions]:

[Follow Up]:

[The topics given by LC change and change]:

 [Code style]:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325047718&siteId=291194637