Python 3 solution


  • 0
    B
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            if not nums:
                return -1
            start = 0
            end = len(nums) - 1
    
            while start <= end:
                mid = (end + start) // 2
    
                if nums[mid] == target:
                    return mid
                if nums[start] <= nums[mid]:  # left is sorted
                    if nums[start] <= target <= nums[mid]:
                        end = mid - 1
                    else:
                        start = mid + 1
                else: 
                    if nums[mid] < nums[end]:  # right part is sorted
                        if nums[mid] <= target <= nums[end]:
                            start = mid + 1
                        else:
                            end = mid - 1
            return -1
    

Log in to reply
 

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