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.
You may assume no duplicate exists in the array.
这个就是一个比较绕的二分。
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums[-1]>=nums[0]:
return nums[0]
l, r, m =0,len(nums)-1,0
while l<=r:
m = (l+r)/2
if r-l == 1:
return min(nums[l],nums[r])
if nums[m] > nums[l]:
l = m
elif nums[m] < nums[r]:
r = m
return nums[m]