K Closest Numbers In Sorted Array

寻找最接近的K个数
本文介绍了一种高效算法来找到已排序数组中离目标值最近的K个数。该算法分为两部分:首先使用二分查找找到最接近目标值的数;然后通过双指针法找出其余K-1个最接近的数。文章提供了详细的实现代码,并讨论了一些特殊情况。

Important Node:

1) This problem has two parts: 

a. find the closest number's to the target and return its index. This is standard binary search problem. O(logN)

b. find kth closest number. This is two pointers problem. O(k)

in this b part, be careful about the boundary. j should be less than nums.length instead of k, because rstIdx < k already control it. 

2) Near to pay attension to coner case when k is equal to 0, just return an empty array instead of null

3) rst[rstIdx++] => rst[rstIdx] then rstIdx++

rst[++rstIdx] => rstIdx++ then rst[rstIdx(the one that already increased)]

public class Solution {
    /**
     * @param A an integer array
     * @param target an integer
     * @param k a non-negative integer
     * @return an integer array
     */
    public int[] kClosestNumbers(int[] A, int target, int k) {
        // Write your code here
        if (A == null || A.length == 0) {
            return null;
        }
        
        if (k == 0) {
            return new int[0];
        }
        
        //find the nearst number
        int nearIdx = findNearestIdx(A, target); 
        
        //find the first Kth closest number
        int[] rst = findKthNearest(A, k, target, nearIdx); 
        
        return rst;
    }
    
    private int findNearestIdx(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        
        while (start + 1  < end) {
            int mid = start + (end - start) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        
        int startDis = Math.abs(nums[start] - target);
        int endDis = Math.abs(nums[end] - target);
        
        return startDis <= endDis ? start : end;
    }
    
    private int[] findKthNearest(int[] nums, int k, int target, int nearIdx) {
        int[] rst = new int[k];
        rst[0] = nums[nearIdx];
        int rstIdx = 1;
        int i = nearIdx - 1, j = nearIdx + 1;
        while (rstIdx < k && i >=0 && j < nums.length) {
            int disi = Math.abs(target - nums[i]);
            int disj = Math.abs(target - nums[j]);
            if (disi <= disj) {
                rst[rstIdx++] = nums[i--];
            } else {
                rst[rstIdx++] = nums[j++];
            }
        }
        
        while (rstIdx < k && i >= 0) {
            rst[rstIdx++] = nums[i--];
        }
        
        while (rstIdx < k && j < nums.length) {
            rst[rstIdx++] = nums[j++];
        }
        
        return rst;
    } 
    
}

 

Method Two: 

1) Find the first Index that is larger than target

2) Using two pointers to find Kth number. All the index that is less than the "first Index", use target - nums[i]

Code: 
http://www.jiuzhang.com/solutions/k-closest-numbers-in-sorted-array/

转载于:https://www.cnblogs.com/codingEskimo/p/6828846.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值