美文网首页
Find Minimum in Rotated Sorted A

Find Minimum in Rotated Sorted A

作者: 穿越那片海 | 来源:发表于2017-09-03 18:00 被阅读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]
    

    相关文章

      网友评论

          本文标题:Find Minimum in Rotated Sorted A

          本文链接:https://www.haomeiwen.com/subject/nnejjxtx.html