53-2-0 ~ n-1 in the absence of digital

Title: a length of the array all numbers in ascending order in the n-1 is unique, and each number in the range of 0 ~ n-1. 0 ~ n 1-n numbers within the range, and there is only one number is not in the array, find this number.

def get_missing_num(nums):
    if len(nums)<1:
        return -1
    begin,end = 0,len(nums)-1
    while begin<end:
        mid = (begin+end)//2
        if nums[mid] == mid:
            begin = mid
        else:
            end = mid
        if begin+1==end:
            break
    if nums[begin]+1==nums[end]:
        return 0
    else:
        return nums[begin]+1

  Note: Use binary mode looking for, if in the middle of the number and index the same, indicating that numbers missing on the right, so begin to point to this position; if the middle of the number of the index is not the same, indicating that numbers missing on the left, so that end point to this location . Eventually find two numbers begin and end adjacent. If the difference between these two numbers is also 1, indicating the first digit missing number 0, otherwise, the missing number of these two digital numbers in the middle.

Guess you like

Origin www.cnblogs.com/kingshine007/p/11502906.html