Skip to content

Commit

Permalink
add: 回旋镖的数量
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 8, 2024
1 parent a691655 commit d2a958d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [回旋镖的数量](src/array/number_of_boomerangs.rs) [数组, 哈希表, 数学]

- LeetCode 447. 回旋镖的数量 <https://leetcode.cn/problems/number-of-boomerangs>

- [经营摩天轮的最大利润](src/array/maximum_profit_of_operating_a_centennial_wheel.rs) [数组, 模拟]

- LeetCode 1599. 经营摩天轮的最大利润 <https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel>
Expand Down
Binary file added images/array/number_of_boomerangs.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ pub mod house_robber;
pub mod stamping_the_grid;
pub mod min_cost_climbing_stairs;
pub mod maximum_profit_of_operating_a_centennial_wheel;
pub mod number_of_boomerangs;
28 changes: 28 additions & 0 deletions src/array/number_of_boomerangs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 回旋镖的数量
// https://leetcode.cn/problems/number-of-boomerangs
// INLINE ../../images/array/number_of_boomerangs.jpeg

pub struct Solution;

impl Solution {
fn distance(p1: &Vec<i32>, p2: &Vec<i32>) -> i32 {
(p1[0] - p2[0]).pow(2) + (p1[1] - p2[1]).pow(2)
}
pub fn number_of_boomerangs(points: Vec<Vec<i32>>) -> i32 {
let mut res = 0;
for i in 0..points.len() {
let mut map = std::collections::HashMap::new();
for j in 0..points.len() {
if i != j {
let distance = Solution::distance(&points[i], &points[j]);
let count = map.entry(distance).or_insert(0);
*count += 1;
}
}
for (_, v) in map {
res += v * (v - 1);
}
}
res
}
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ pub mod house_robber_test;
pub mod stamping_the_grid_test;
pub mod min_cost_climbing_stairs_test;
pub mod maximum_profit_of_operating_a_centennial_wheel_test;
pub mod number_of_boomerangs_test;
23 changes: 23 additions & 0 deletions tests/array/number_of_boomerangs_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use rust_practice::array::number_of_boomerangs::Solution;

#[test]
fn number_of_boomerangs() {
// 示例 1:
// 输入:points = [[0,0],[1,0],[2,0]]
// 输出:2
// 解释:两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]
let points = vec![vec![0, 0], vec![1, 0], vec![2, 0]];
assert_eq!(Solution::number_of_boomerangs(points), 2);

// 示例 2:
// 输入:points = [[1,1],[2,2],[3,3]]
// 输出:2
let points = vec![vec![1, 1], vec![2, 2], vec![3, 3]];
assert_eq!(Solution::number_of_boomerangs(points), 2);

// 示例 3:
// 输入:points = [[1,1]]
// 输出:0
let points = vec![vec![1, 1]];
assert_eq!(Solution::number_of_boomerangs(points), 0);
}

0 comments on commit d2a958d

Please sign in to comment.