标签: LeetCodepython
题目地址:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/web
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).svg
Find the minimum element.spa
You may assume no duplicate exists in the array.code
找出旋转有序数组中的最小值。xml
这个题是剑指offer上的原题,这里在复习一下。看到有序的数组就想到二分查找。这个是变种而已。blog
注意边界和循环条件。图片
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]
2018 年 3 月 12 日 ip