LeetCode 542. 01 Matrix
考点 | 难度 |
---|---|
Matrix | Medium |
题目
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
思路
BFS:
所有zero cell的distance都是0
queue储存(r, c) 用来process zero cells的unvisited neighbors, dir是(0, 1) (1, 0) (0, -1) (-1, 0)
答案
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int