Search for the position target-0.5, share my short JAVA solution


  • 0
    T
    public int searchInsert(int[] A, int target) {
        int start = 0;
        int end = A.length - 1;
        while (start < end) {
            int mid = start + (end - start) / 2;
            if (target - 0.5 < A[mid])
                end = mid;
            else
                start = mid + 1;
        }
        return A[start] >= target ? start : A.length;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.