-
Notifications
You must be signed in to change notification settings - Fork 9
/
island_perimeter.rs
43 lines (42 loc) · 1.22 KB
/
island_perimeter.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/// https://leetcode.com/problems/island-perimeter/
/// 逐行遍历grid中所有为1的格子,遇到一个1就往上下左右四个方向延伸,遇到边界或0就周长加一,遇到1则不加
fn island_perimeter(grid: Vec<Vec<i32>>) -> i32 {
let (m, n) = (grid.len(), grid[0].len());
let mut perimeter = 0;
for i in 0..m {
for j in 0..n {
if grid[i][j] == 0 {
continue;
}
// up and down
if i == 0 || grid[i - 1][j] == 0 {
perimeter += 1;
}
if i == m - 1 || grid[i + 1][j] == 0 {
perimeter += 1;
}
// left and right
if j == 0 || grid[i][j - 1] == 0 {
perimeter += 1;
}
if j == n - 1 || grid[i][j + 1] == 0 {
perimeter += 1;
}
}
}
perimeter
}
#[test]
fn test_island_perimeter() {
#[rustfmt::skip]
let test_cases = vec![(
vec_vec![[0, 1, 0, 0],
[1, 1, 1, 0],
[0, 1, 0, 0],
[1, 1, 0, 0]],
16,
)];
for (grid, perimeter) in test_cases {
assert_eq!(island_perimeter(grid), perimeter);
}
}