Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 34d0d7a

Browse files
author
guangsheng.li01
committed
Solved 0973
1 parent 32ca543 commit 34d0d7a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎src/a0973_k_closest_points_to_origin.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* [0973] k-closest-points-to-origin
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn k_closest(points: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
11+
let mut points = points;
12+
points.sort_by_key(|p| p[0] * p[0] + p[1] * p[1]);
13+
let mut closest_k = Vec::new();
14+
for i in 0..k as usize {
15+
closest_k.push(points[i].clone());
16+
}
17+
closest_k
18+
}
19+
}
20+
21+
// solution impl ends here
22+
23+
// solution tests starts here
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[test]
30+
fn test_case0() {
31+
assert_eq!(
32+
Solution::k_closest(vec![vec![1, 3], vec![-2, 2]], 1),
33+
vec![[-2, 2]]
34+
);
35+
}
36+
}
37+
38+
// solution tests ends here

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod a0867_transpose_matrix;
2727
mod a0929_unique_email_addresses;
2828
mod a0931_minimum_falling_path_sum;
2929
mod a0937_reorder_data_in_log_files;
30+
mod a0973_k_closest_points_to_origin;
3031
mod a1021_remove_outermost_parentheses;
3132
mod a1047_remove_all_adjacent_duplicates_in_string;
3233
mod a1200_minimum_absolute_difference;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /