Skip to content

Commit

Permalink
leetcode: Add 0463
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 27, 2024
1 parent af8ff5a commit 22b8cf1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
- [0374. 猜数字大小 Guess Number Higher or Lower](leetcode/0374.guess-number-higher-or-lower/index.md)
- [0394. 字符串解码 Decode String](leetcode/0394.decode-string/index.md)
- [0401-0500](leetcode/by-id/0401-0500.md)
- [0463. 岛屿的周长 Island Perimeter](leetcode/0463.island-perimeter/index.md)
- [0468. 验证IP地址 Validate IP Address](leetcode/0468.validate-ip-address/index.md)
- [0485. 最大连续1的个数 Max Consecutive Ones](leetcode/0485.max-consecutive-ones/index.md)
- [0496. 下一个更大元素 I Next Greater Element I](leetcode/0496.next-greater-element-i/index.md)
Expand Down
7 changes: 7 additions & 0 deletions src/leetcode/0463.island-perimeter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-0463-island-perimeter"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
4 changes: 4 additions & 0 deletions src/leetcode/0463.island-perimeter/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# 0463. 岛屿的周长 Island Perimeter

[问题描述](https://leetcode.com/problems/island-perimeter)
87 changes: 87 additions & 0 deletions src/leetcode/0463.island-perimeter/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

// Brute force
// 时间复杂度 O(m * n)
pub fn island_perimeter1(grid: Vec<Vec<i32>>) -> i32 {
// 一个岛屿有多个方格组成, 一个方格有四个边.
// 对于任意一个边, 当与组成岛屿的另一个方格相邻时, 在计算岛的周长时,
// 就不需要计算这个边;
// 当不与组成岛屿的另一个方格相邻时, 在计算周长时, 就要算上这个边.
// 一个岛域的周长, 就是这些方格的边的总和.
// 遍历所有方格, 检查它是不是岛屿的一部分.
// 如果是, 就检查它的四个边, 是否能作为岛域周长的一部分.

const IS_ISLAND: i32 = 1;
const IS_NOT_ISLAND: i32 = 0;
let rows = grid.len();
assert!((1..=100).contains(&rows));
let columns = grid[0].len();
assert!((1..=100).contains(&columns));
let rows_i32 = rows as i32;
let columns_i32 = columns as i32;
// 可能的四个方向.
let directions = [(0_i32, -1_i32), (-1, 0), (1, 0), (0, 1)];

let mut sum = 0;
for row in 0..rows {
for column in 0..columns {
if grid[row][column] == IS_ISLAND {
// 尝试四个边
for dir in directions {
let row1 = row as i32 + dir.0;
let column1 = column as i32 + dir.1;
if 0 <= row1 && row1 < rows_i32 && 0 <= column1 && column1 < columns_i32 {
// 如果相邻的方格是 grid 内部
let row1 = row1 as usize;
let column1 = column1 as usize;

// 并且它不是岛屿的一部分.
if grid[row1][column1] == IS_NOT_ISLAND {
sum += 1;
}
} else {
// 如果相邻的方格不是在 grid 内部, 说明这个边可以作为岛屿周长的一部分.
sum += 1;
}
}
}
}
}

// 返回结果
sum
}

pub type SolutionFn = fn(Vec<Vec<i32>>) -> i32;

fn check_solution(func: SolutionFn) {
let grid = vec![
vec![0, 1, 0, 0],
vec![1, 1, 1, 0],
vec![0, 1, 0, 0],
vec![1, 1, 0, 0],
];
assert_eq!(func(grid), 16);

let grid = vec![vec![1]];
assert_eq!(func(grid), 4);

let grid = vec![vec![1, 0]];
assert_eq!(func(grid), 4);
}

fn main() {
check_solution(island_perimeter1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, island_perimeter1};

#[test]
fn test_island_perimeter1() {
check_solution(island_perimeter1);
}
}
3 changes: 2 additions & 1 deletion src/leetcode/tags/array/matrix.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# 矩阵 Matrix

## 简单
1. [0463. 岛屿的周长 Island Perimeter](../../0463.island-perimeter/index.md)

## 中等

## 困难
## 困难

0 comments on commit 22b8cf1

Please sign in to comment.