542. 01 Matrix Medium

@problem@discussion
#Array#Dynamic Programming#Breadth-First Search#Matrix



1/**
2 * [542] 01 Matrix
3 *
4 * Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
5 * The distance between two adjacent cells is 1.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;" />
9 * Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
10 * Output: [[0,0,0],[0,1,0],[0,0,0]]
11 * 
12 * Example 2:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;" />
14 * Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
15 * Output: [[0,0,0],[0,1,0],[1,2,1]]
16 * 
17 *  
18 * Constraints:
19 * 
20 * 	m == mat.length
21 * 	n == mat[i].length
22 * 	1 <= m, n <= 10^4
23 * 	1 <= m * n <= 10^4
24 * 	mat[i][j] is either 0 or 1.
25 * 	There is at least one 0 in mat.
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/01-matrix/
31// discuss: https://leetcode.com/problems/01-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn update_matrix(mat: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
37        vec![]
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_542() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.