-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |