首页 > 代码库 > Closest Number in Sorted Array

Closest Number in Sorted Array

public class Solution {
    /**
     * @param A an integer array sorted in ascending order
     * @param target an integer
     * @return an integer
     */
    public int closestNumber(int[] A, int target) {
        // Write your code here
        if (A == null || A.length == 0) {
            return -1;
        }
        
        int start = 0;
        int end = A.length - 1;
        
        while(start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (A[mid] == target) {
                return mid;
            } else if (A[mid] < target) {
                start = mid;
            } else {
                end = mid;
            }
        }
        
        int startDis = Math.abs(A[start] - target);
        int endDis = Math.abs(A[end] - target);
        return startDis < endDis? start : end;
        
    }
}

O(logN)

Closest Number in Sorted Array