Skip to content

Commit b2f46d0

Browse files
committedMay 28, 2019
2019-05-28
1 parent 940a899 commit b2f46d0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from collections import deque
2+
class Solution(object):
3+
def floodFill(self, image, sr, sc, newColor):
4+
"""
5+
:type image: List[List[int]]
6+
:type sr: int
7+
:type sc: int
8+
:type newColor: int
9+
:rtype: List[List[int]]
10+
"""
11+
m, n = len(image), len(image[0])
12+
color = image[sr][sc]
13+
image[sr][sc] = newColor
14+
15+
visited = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
16+
dx = [1, -1, 0, 0]
17+
dy = [0, 0, 1, -1]
18+
19+
q = deque()
20+
q.append([sr,sc])
21+
while q:
22+
x0, y0 = q.popleft()
23+
for k in range(4):
24+
x = x0 + dx[k]
25+
y = y0 + dy[k]
26+
27+
if 0 <= x < m and 0 <= y < n and image[x][y] == color and visited[x][y] == 0:
28+
image[x][y] = newColor
29+
visited[x][y] = 1
30+
q.append([x, y])
31+
32+
return image

0 commit comments

Comments
 (0)