153. Find Minimum in Rotated Sorted Array 寻找旋转排序数组中的最小值

题目地址:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/

题目描述:

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

Youmay assume no duplicate exists in the array.

题目大意

找出旋转有序数组中的最小值。

解题方法

这个题是剑指offer上的原题,这里在复习一下。看到有序的数组就想到二分查找。这个是变种而已。

注意边界和循环条件。

 

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发