lintcode 159. 寻找旋转排序数组中的最小值

2018-08-31  本文已影响2人  cuizixin

难度:中等

1. Description

159. 寻找旋转排序数组中的最小值

2. Solution

class Solution:
    """
    @param nums: a rotated sorted array
    @return: the minimum number in the array
    """
    def findMin(self, nums):
        # write your code here
        if nums[-1]>nums[0]:
            return nums[0]
        else:
            start = 0
            end = len(nums)-1
            while end-start>1:
                mid = (start+end)//2
                if nums[mid]<nums[end]:
                    end = mid
                elif nums[mid]>nums[start]:
                    start = mid
            return min(nums[start],nums[end])

3. Reference

  1. https://www.lintcode.com/problem/find-minimum-in-rotated-sorted-array/description
上一篇下一篇

猜你喜欢

热点阅读