Brutal Force
class Solution(object):
def minArray(self, numbers):
"""
:type numbers: List[int]
:rtype: int
"""
if len(numbers) == 0:
return None
if len(numbers) == 1:
return numbers[0]
ret = numbers[0]
for i in range(len(numbers) - 1):
if numbers[i] > numbers[i + 1]:
ret = min(ret, numbers[i + 1])
return ret
双指针法
class Solution(object):
def minArray(self, numbers):
"""
:type numbers: List[int]
:rtype: int
"""
if len(numbers) == 0:
return None
left = 0
right = len(numbers) - 1
# 初始化为0,是为了应对当没有旋转的数组。此时numbers[left] < numbers[right]
# 直接返回numbers[0]。
mid = 0
while numbers[left] >= numbers[right]:
if right - left == 1:
mid = right
break
mid = left + (right - left) // 2
# 应对无法判断的情况
if numbers[left] == numbers[mid] and numbers[mid] == numbers[right]:
return self.searchInOrder(numbers, left, right)
elif numbers[mid] >= numbers[left]:
left = mid
else:
right = mid
return numbers[mid]
def searchInOrder(self, numbers, left, right):
ret = numbers[left]
for i in range(left + 1, right + 1):
if numbers[i] < ret:
ret = numbers[i]
return ret