Find Minimum in Rotated Sorted A

2017-09-03  本文已影响0人  穿越那片海

Medium, Binary Search

Question

Find Minimum in Rotated Sorted Array,
假设有重复数字。

Solution

解法依然是二分搜索,不过多了一种情况要考虑。

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

猜你喜欢

热点阅读