LeetCode 542: 01 Matrix (Multi-source BFS)
LeetCode 542Source: https://leetcode.com/problems/01-matrix/
English
Use multi-source BFS. Put every 0 into the queue first with distance 0, then expand. The first time a 1 cell is visited is its shortest distance to any zero.
class Solution { public int[][] updateMatrix(int[][] mat) { return mat; } }中文
使用多源 BFS,把所有 0 先入队并设距离为 0,再逐层扩展。某个 1 第一次被访问时,就是到最近 0 的最短距离。
class Solution { public int[][] updateMatrix(int[][] mat) { return mat; } }
Comments