forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
40 lines (40 loc) · 1.05 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
List<Integer> res = new ArrayList<>();
if (arr.length < k) {
for (int item : arr) {
res.add(item);
}
return res;
}
int left = 0, right = arr.length - 1;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (arr[mid] > x) {
right = mid - 1;
} else {
left = mid;
}
}
int left1 = 0;
int right1 = arr.length - 1;
if (left >= k) {
left1 = left - k;
}
if (arr.length - 1 - left >= k) {
right1 = left + k;
}
while (right1 - left1 >= k) {
if (Math.abs(arr[left1] - x) > Math.abs(arr[right1] -x)) {
left1++;
} else {
right1--;
}
}
while (left1 <= right1) {
res.add(arr[left1]);
left1++;
}
return res;
}
}