python small modification to Search in Rotated Sorted Array


  • 0
    P

    just added below statements, and make elif below this line to if.
    if nums[start] == nums[end] and nums[start] != target:
    end -= 1

    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: bool
            """
            if not nums:
                return False
            start = 0;end = len(nums)-1
            while start <= end:
                mid = start+(end-start)/2
                if nums[mid] == target:
                    return True
                if nums[start] == nums[end] and nums[start] != target:
                    end -= 1
                elif nums[start] <= nums[mid]:
                    if nums[start] <= target <= nums[mid]:
                        end = mid - 1
                    else:
                        start = mid + 1
                else:
                    if nums[mid] <= target <= nums[end]:
                        start = mid + 1
                    else:
                        end = mid
            return False
    

Log in to reply
 

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