美文网首页
154. Find Minimum in Rotated Sor

154. Find Minimum in Rotated Sor

作者: April63 | 来源:发表于2018-05-15 14:17 被阅读0次

    上一题是没有重复的数字,这一题有重复数字,完全和offer上一样。代码如下:

    class Solution(object):
        def findMin(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            if len(nums) == 0:
                return -1
            if len(nums) == 1:
                nums[0]
            left = 0
            right = len(nums) - 1
            print left
            print right
            if nums[left] < nums[right]:
                return nums[left]
            while nums[left] >= nums[right]:
                if right - left == 1:
                    mid = right
                    break
                mid = (left + right) / 2
                print mid
                if nums[left] == nums[mid] and nums[mid] == nums[right]:
                    result = nums[left]
                    for i in range(left, right+1):
                        if nums[i] < result:
                            result = nums[i]
                    return result
                elif nums[left] <= nums[mid]:
                    left = mid
                elif nums[mid] <= nums[right]:
                    right = mid
            return nums
    

    相关文章

      网友评论

          本文标题:154. Find Minimum in Rotated Sor

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