Java Binary search O(logn)


  • 0
    X

    '''
    public int searchInsert(int[] nums, int target) {
    int l = 0;
    int r = nums.length - 1;
    int mid = 0;
    while (l <= r) {
    mid = l + (r - l) / 2;
    if (target == nums[mid]) return mid;
    if (target < nums[mid]) {
    r = mid - 1;
    } else {
    l = mid + 1;
    }
    }
    return l;
    }
    '''


Log in to reply
 

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