7/08/2023

binary search problem

 Generic Binary Search Snippet

left = 0
right = len(nums)-1
while left <= right:
    #update mid
    mid = left+(right-left)//2
    if "find the element"
        do something # return mid index
    elif "cut the left half"
        left = mid+1
    else "cut the right half"
        right = mid-1
return "no found" # if not found, return left index

没有评论: