1020. Number of Enclaves Medium

@problem@discussion
#Array#Depth-First Search#Breadth-First Search#Union Find#Matrix



1/**
2 * [1020] Number of Enclaves
3 *
4 * You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.
5 * A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.
6 * Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves1.jpg" style="width: 333px; height: 333px;" />
10 * Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
11 * Output: 3
12 * Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
13 * 
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/enclaves2.jpg" style="width: 333px; height: 333px;" />
16 * Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
17 * Output: 0
18 * Explanation: All 1s are either on the boundary or can reach the boundary.
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	m == grid.length
24 * 	n == grid[i].length
25 * 	1 <= m, n <= 500
26 * 	grid[i][j] is either 0 or 1.
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/number-of-enclaves/
32// discuss: https://leetcode.com/problems/number-of-enclaves/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn num_enclaves(grid: Vec<Vec<i32>>) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_1020() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.