K Closest In Sorted Array - Medium

Given a target integer T, a non-negative integer K and an integer array A sorted in ascending order, find the K closest numbers to T in A.

Assumptions

  • A is not null
  • K is guranteed to be >= 0 and K is guranteed to be <= A.length

Return

  • A size K integer array containing the K closest numbers(not indices) in A, sorted in ascending order by the difference between the number and T.

Examples

  • A = {1, 2, 3}, T = 2, K = 3, return {2, 1, 3} or {2, 3, 1}
  • A = {1, 4, 6, 8}, T = 3, K = 3, return {4, 1, 6}

和leetcode 658. Find K Closest Elements  https://www.cnblogs.com/fatttcat/p/10062228.html

不同的地方是,本题要求输出的array是按与target的距离排序

time: O(log(n) + k), space: O(1)

public class Solution {
  public int[] kClosest(int[] array, int target, int k) {
    // Write your solution here
    int left = 0, right = array.length - 1;
    while(left + 1 < right) {
      int mid = left + (right- left) / 2;
      if(array[mid] >= target)
        right = mid;
      else
        left = mid;
    }
    int tmp;
    if(array[right] <= target)
      tmp = right;
    if(array[left] <= target)
      tmp = left;
    else
      tmp = -1;

    left = tmp;
    right = left + 1;

    int[] res = new int[k];
    for(int i = 0; i < k; i++) {
      if(left >= 0 && right <= array.length - 1 && Math.abs(array[left] - target) <= Math.abs(array[right] - target))
        res[i] = array[left--];
      else if(left >= 0 && right > array.length - 1)
        res[i] = array[left--];
      else
        res[i] = array[right++];
    }
    return res;
  }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10126564.html

时间: 2024-07-31 11:26:16

K Closest In Sorted Array - Medium的相关文章

Closest In Sorted Array - Medium

Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T. Assumptions There can be duplicate elements in the array, and we can return any of the indices with same value. Examples

973. K Closest Points to Origin - Medium

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

Closest In Sorted Array

Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T. Assumptions There can be duplicate elements in the array, and we can return any of the indices with same value. Examples

33. Search in Rotated Sorted Array - Medium

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1.

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 ord

62. Search in Rotated Sorted Array【medium】

62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, ot

658. Find K Closest Elements

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp

leetcode 658. Find K Closest Elements

Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred. Example 1: Input: [1,2,3,4,5], k=4, x=3 Outp

leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array

问题:Remove Duplicates from Sorted Array II 难度:medium Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2