边工作边刷题:70天一遍leetcode: day 8

Find Minimum in Rotated Sorted Array

要点:low<=high:因为1 or 2元素的情况都做特殊处理,所以low<=high可以保证进loop。

class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        low = 0
        high = len(nums)-1
        while low<=high:
            if high-low<2:
                return nums[low] if high-low==0 else min(nums[low],nums[high])
            if nums[low]<nums[high]:
                return nums[low]

            mid = low + (high-low)/2
            if nums[mid]>=nums[low]:
                low = mid+1
            else:
                high = mid
        return nums[mid]
原文地址:https://www.cnblogs.com/absolute/p/5675736.html