K Closest Numbers In Sorted Array
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K Closest Numbers In Sorted Array相关的知识,希望对你有一定的参考价值。
Given a target number, a non-negative integer k
and an integer array A sorted in ascending order, find the k closest numbers to target in A, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending order by number if the difference is same.
Given A = [1, 2, 3]
, target = 2
and k = 3
, return [2, 1, 3]
.
Given A = [1, 4, 6, 8]
, target = 3
and k = 3
, return [4, 1, 6]
.
Solution:
/* findFirst() is to find the first index of number that is larger or equal to target, the way is binary search
* "target - A[start] >= A[end] - target" is keep order ascending when left and right have same diff
*/
public int[] kClosestNumbers(int[] A, int target, int k) {
int[] ret = new int[k];
if (A == null || A.length < k) {
return ret;
}
int start = findFirst(A, target) - 1;
int end = start + 1;
for (int i = 0; i < k; i++) {
if (start < 0) {
ret[i] = A[end++];
} else if (end >= A.length) {
ret[i] = A[start--];
} else {
if (target - A[start] >= A[end] - target) {
ret[i] = A[start--];
} else {
ret[i] = A[end++];
}
}
}
return ret;
}
private int findFirst(int[] A, int target) {
if (A == null || A.length == 0) {
return -1;
}
int start = 0;
int end = A.length - 1;
int mid = 0;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (A[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (A[start] >= target) {
return start;
} else if (A[end] >= target) {
return end;
} else {
return A.length;
}
}
以上是关于K Closest Numbers In Sorted Array的主要内容,如果未能解决你的问题,请参考以下文章
LF.19.K Closest In Sorted Array
HDU - 6621 K-th Closest Distance 主席树+二分答案
Microsoft - Find the K closest points to the origin in a 2D plane
Leetcode 743. Closest Leaf in a Binary Tree