1254. Number of Closed Islands Medium

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



1/**
2 * [1254] Number of Closed Islands
3 *
4 * Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally connected group of <font face="monospace">0</font>s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.
5 * Return the number of closed islands.
6 *  
7 * Example 1:
8 * <img alt="" src="https://assets.leetcode.com/uploads/2019/10/31/sample_3_1610.png" style="width: 240px; height: 120px;" />
9 * 
10 * Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
11 * Output: 2
12 * Explanation: 
13 * Islands in gray are closed because they are completely surrounded by water (group of 1s).
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2019/10/31/sample_4_1610.png" style="width: 160px; height: 80px;" />
16 * 
17 * Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
18 * Output: 1
19 * 
20 * Example 3:
21 * 
22 * Input: grid = [[1,1,1,1,1,1,1],
23 *                [1,0,0,0,0,0,1],
24 *                [1,0,1,1,1,0,1],
25 *                [1,0,1,0,1,0,1],
26 *                [1,0,1,1,1,0,1],
27 *                [1,0,0,0,0,0,1],
28 *                [1,1,1,1,1,1,1]]
29 * Output: 2
30 * 
31 *  
32 * Constraints:
33 * 
34 * 	1 <= grid.length, grid[0].length <= 100
35 * 	0 <= grid[i][j] <=1
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/number-of-closed-islands/
41// discuss: https://leetcode.com/problems/number-of-closed-islands/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn closed_island(grid: Vec<Vec<i32>>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_1254() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.