Commented Python Solution


  • 0
    class Solution(object):
        def search(self, nums, target):
            if not nums:
                return False
            lo, hi = 0, len(nums)-1
            while lo <= hi:
                mid = (lo + hi)/2
                if nums[mid] == target:
                    return True
                # pivot is on the right side
                if nums[lo] < nums[mid]:
                    # is it in the range of the left side?
                    if nums[lo] <= target <= nums[mid]:
                        hi = mid - 1
                    else:
                        lo = mid + 1
                # pivot is on the left side
                elif nums[lo] > nums[mid]:
                    # is it in the range of the right side?
                    if nums[mid] <= target <= nums[hi]:
                        lo = mid + 1
                    else:
                        hi = mid - 1
                # don't know where pivot is - we must move lo pointer forward
                else:
                    lo += 1
            return False
    

Log in to reply
 

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