Easy to follow python solution


  • 0
    Y
    class Solution(object):
        def search(self, nums, target):
            if not nums:return False
            l=0
            r=len(nums)-1
            while l<=r:
                while l+1<=r:
                    if nums[l]==nums[l+1]:
                        l+=1
                    else:
                         break
                while l<=r-1:
                    if nums[r]==nums[r-1]:
                        r-=1
                    else:
                        break
                mid=(l+r)/2
                if nums[mid]==target:return True
                if nums[l]<=nums[mid]:
                    if target<nums[mid] and target>=nums[l]:
                        r=mid-1
                    else:
                        l=mid+1
                else:
                    if target>nums[mid] and target<=nums[r]:
                        l=mid+1
                    else:
                        r=mid-1
            return False
    

Log in to reply
 

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