代码拉取完成,页面将自动刷新
# Time: O(m * n)
# Space: O(m + n)
import collections
class Solution(object):
def colorBorder(self, grid, r0, c0, color):
"""
:type grid: List[List[int]]
:type r0: int
:type c0: int
:type color: int
:rtype: List[List[int]]
"""
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
lookup, q, borders = set([(r0, c0)]), collections.deque([(r0, c0)]), []
while q:
r, c = q.popleft()
is_border = False
for direction in directions:
nr, nc = r+direction[0], c+direction[1]
if not ((0 <= nr < len(grid)) and \
(0 <= nc < len(grid[0])) and \
grid[nr][nc] == grid[r][c]):
is_border = True
continue
if (nr, nc) in lookup:
continue
lookup.add((nr, nc))
q.append((nr, nc))
if is_border:
borders.append((r, c))
for r, c in borders:
grid[r][c] = color
return grid
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。