Share simple Python Binary Search code


  • 0
    W
    class Solution(object):
        def findMin(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n=len(nums)
            if n==1:
                return nums[0]
            s,l=0,n-1
            while l>s+2:
                m=(l+s)/2
                if nums[m]>nums[s] and nums[m]<nums[l]:
                    return nums[s]
                if nums[m]<nums[s]:
                    l=m
                elif nums[m]>nums[s]:
                    s=m
            for i in range(s,l):
                if nums[i+1]<nums[i]:
                    return nums[i+1]
            return nums[s]

Log in to reply
 

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