We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5348012 commit a2f14cfCopy full SHA for a2f14cf
solution/0900-0999/0973.K Closest Points to Origin/Solution.rs
@@ -1,7 +1,10 @@
1
impl Solution {
2
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()
+ points.sort_by(|a, b| {
+ let dist_a = f64::hypot(a[0] as f64, a[1] as f64);
+ 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()
9
}
10
0 commit comments