154. Find Minimum in Rotated Sor

2018-05-15  本文已影响0人  April63

上一题是没有重复的数字,这一题有重复数字,完全和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
上一篇下一篇

猜你喜欢

热点阅读