K Closest Numbers In Sorted Array

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值