Find Minimum in Rotated Sorted Array

Medium, Binary Search

Question

假设升序序列在某个点被旋转了,寻找最小数。

For Example
(0 1 2 4 5 6 7旋转以后成为 4 5 6 7 0 1 2).

Notes

假设序列中无重复数字

Solution

使用二分搜索。

class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        L, R = 0, len(nums)-1
        while L nums[R]:
            M = (L+R)/2
            if nums[M] > nums[R]:
                L = M+1
            else:
                R = M
        return nums[L]

你可能感兴趣的:(Find Minimum in Rotated Sorted Array)