Skip to content

Commit a2f14cf

Browse files
authored
Update Solution.rs
1 parent 5348012 commit a2f14cf

File tree

1 file changed

+6
-3
lines changed
  • solution/0900-0999/0973.K Closest Points to Origin

1 file changed

+6
-3
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
impl Solution {
22
pub fn k_closest(mut points: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {
3-
points
4-
.sort_unstable_by(|a, b| (a[0].pow(2) + a[1].pow(2)).cmp(&(b[0].pow(2) + b[1].pow(2))));
5-
points[0..k as usize].to_vec()
3+
points.sort_by(|a, b| {
4+
let dist_a = f64::hypot(a[0] as f64, a[1] as f64);
5+
let dist_b = f64::hypot(b[0] as f64, b[1] as f64);
6+
dist_a.partial_cmp(&dist_b).unwrap()
7+
});
8+
points.into_iter().take(k as usize).collect()
69
}
710
}

0 commit comments

Comments
 (0)