forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflood-fill.py
More file actions
27 lines (21 loc) · 744 Bytes
/
flood-fill.py
File metadata and controls
27 lines (21 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
stack = [(sr, sc)]
originColor = image[sr][sc]
while stack:
i, j = stack.pop()
if image[i][j]==newColor or image[i][j]!=originColor: continue
image[i][j] = newColor
if i+1<len(image): stack.append((i+1, j))
if 0<=i-1: stack.append((i-1, j))
if j+1<len(image[0]): stack.append((i, j+1))
if 0<=j-1: stack.append((i, j-1))
return image
"""
DFS the `image`.
If the color is newColor, we don't need to see.
If the color is not originalColor, we don't need to see, too.
Else we paint the node to newColor.
Time: O(N).
Space: O(1).
"""