Python Binary Search


  • 0
    1. figure out is the target on the first increasing or second increasing subsequence.
      2.decide change right or left
    class Solution(object):
        def search(self, nums, target):
            left, right = 0, len(nums) - 1
            while left <= right:
                mid = (left + right) >> 1
                if nums[mid] == target:
                    return mid
                if target >= nums[0]:
                    if nums[mid] < target and nums[mid] >= nums[0]:
                        left = mid + 1
                    else:
                        right = mid - 1
                else:
                    if nums[mid] < target or nums[mid] >= nums[0]:
                        left = mid + 1
                    else:
                        right = mid - 1
            return -1
    

Log in to reply
 

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