658. Find K Closest Elements Medium
1/**
2 * [658] Find K Closest Elements
3 *
4 * Given a sorted integer array arr, two integers k and x, return the k closest integers to x in the array. The result should also be sorted in ascending order.
5 * An integer a is closer to x than an integer b if:
6 *
7 * |a - x| < |b - x|, or
8 * |a - x| == |b - x| and a < b
9 *
10 *
11 * Example 1:
12 * Input: arr = [1,2,3,4,5], k = 4, x = 3
13 * Output: [1,2,3,4]
14 * Example 2:
15 * Input: arr = [1,2,3,4,5], k = 4, x = -1
16 * Output: [1,2,3,4]
17 *
18 * Constraints:
19 *
20 * 1 <= k <= arr.length
21 * 1 <= arr.length <= 10^4
22 * arr is sorted in ascending order.
23 * -10^4 <= arr[i], x <= 10^4
24 *
25 */
26pub struct Solution {}
27
28// problem: https://leetcode.com/problems/find-k-closest-elements/
29// discuss: https://leetcode.com/problems/find-k-closest-elements/discuss/?currentPage=1&orderBy=most_votes&query=
30
31// submission codes start here
32
33impl Solution {
34 pub fn find_closest_elements(arr: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
35 vec![]
36 }
37}
38
39// submission codes end
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44
45 #[test]
46 fn test_658() {
47 }
48}
49
Back
© 2025 bowen.ge All Rights Reserved.