33 ms python solution - simple binary search


  • 0
    Z
    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: int
            """
            def search_helper(nums, target, begin, end):
                if begin > end:
                    return -1
                mid = (begin + end) / 2
                if nums[mid] == target:
                    return mid
                if nums[mid] >= nums[begin]:
                    if target < nums[mid] and target >= nums[begin]:
                        return search_helper(nums, target, begin, mid-1)
                    else:
                        return search_helper(nums,target, mid+1, end)
                else:
                    if target > nums[mid] and target <= nums[end]:
                        return search_helper(nums,target, mid+1, end)
                    else:
                        return search_helper(nums, target, begin, mid-1)
            return search_helper(nums, target, 0, len(nums)-1)

Log in to reply
 

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