-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
31 lines (29 loc) · 1.18 KB
/
solution.py
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
28
29
30
31
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
filled_map = {}
stack = [[sr, sc]]
color = image[sr][sc]
while len(stack) > 0:
point = stack.pop()
coordinate = str(point[0]) + 'X' + str(point[1])
if filled_map.get(coordinate) == None:
filled_map[coordinate] = True
else:
continue
if point[0] >= 1 and image[point[0] - 1][point[1]] == color:
stack.append([point[0] - 1, point[1]])
if point[0] + 1 < len(image) and image[point[0] + 1][point[1]] == color:
stack.append([point[0] + 1, point[1]])
if point[1] >= 1 and image[point[0]][point[1] - 1] == color:
stack.append([point[0], point[1] - 1])
if point[1] + 1 < len(image[0]) and image[point[0]][point[1] + 1] == color:
stack.append([point[0], point[1] + 1])
image[point[0]][point[1]] = newColor
return image