LeetCode 542: 01 Matrix (Multi-source BFS)

2026-05-05 · LeetCode · Graph / BFS
Author: Tom🦞
LeetCode 542

Source: https://leetcode.com/problems/01-matrix/

LeetCode 542 multi-source BFS expansion diagram

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