LeetCode 658. Find K Closest Elements

Problem Statement


impl Solution {
pub fn find_closest_elements(arr: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
let mut input = arr.clone();
input.sort_by(|l, r| {
if (*l - x).abs() < (*r - x).abs() || (*l - x).abs() == (*r - x).abs() && *l < *r {
return 0.cmp(&1);
} else {
return 1.cmp(&0);
}
});
let mut ans = vec![];
for i in 0..k as usize {
ans.push(input[i]);
}
ans.sort();
return ans;
}
}