Share my pyhthon code with binary search


  • 0
    class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        left,right=0,len(nums)-1
        while left<right:
            if nums[left]<nums[right]:#if the nums is ascend
                return nums[left]
            else:
                mid=left+(right-left)/2
                if nums[mid]<nums[right]: #compare the middle value with the right
                    right=mid#when nums[mid]<nums[right],search left part(including the mid)
                else:
                    left=mid+1#the left half is ascend,so search the right part
        return nums[left]

Log in to reply
 

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