leetcode -- Find Minimum in Rotated Sorted Array -- 重点

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/

思路:如果是sorted array,要立马想到 binary search。这里要注意bs的条件。

参考:

http://bookshadow.com/weblog/2014/10/16/leetcode-find-minimum-rotated-sorted-array/

class Solution(object):
    def findMin(self, nums):
        """ :type nums: List[int] :rtype: int """
        ans = nums[0]
        size = len(nums)
        low, high = 0, size - 1
        while low <= high:#注意这里是小于等于
            mid = (low + high) / 2
            if nums[mid] <= nums[high]: #min位于上升沿左侧,注意这个条件
                high = mid - 1
            else: #min位于左侧上升沿与右侧上升沿之间
                low = mid + 1
            ans = min(ans, nums[mid])
        return ans

你可能感兴趣的:(LeetCode)