You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).
10
+
#
11
+
#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.
12
+
#
13
+
#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.
14
+
#
15
+
#At the end, return the modified image.
16
+
#
17
+
#Example 1:
18
+
#Input:
19
+
#image = [[1,1,1],[1,1,0],[1,0,1]]
20
+
#sr = 1, sc = 1, newColor = 2
21
+
#Output: [[2,2,2],[2,2,0],[2,0,1]]
22
+
#Explanation:
23
+
#From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
24
+
#by a path of the same color as the starting pixel are colored with the new color.
25
+
#Note the bottom corner is not colored 2, because it is not 4-directionally connected
26
+
#to the starting pixel.
27
+
classSolution:
28
+
deffloodFill(self, image, sr, sc, newColor):
29
+
r, c=len(image), len(image[0]) #Initialize r and c
30
+
clr=image[sr][sc] #Initialize clr which is the location of the color
31
+
ifclr==newColor: #Condition-check: If clr is same as newColor
32
+
returnimage#Then we'll not change the color and return that image
33
+
defdfs(row, column): #Defining dfs accepts row, and column
34
+
ifimage[row][column] ==clr: #Condition-check: If location of that point is same as color
35
+
image[row][column] =newColor#Then we change that to newColor
36
+
ifrow>=1: #Condition-check: If row is greater or equal to 1
37
+
dfs(row-1, column) #Using DFS
38
+
ifrow+1<r: #Condition-check: If row + 1 is less than r
0 commit comments