题解 | #旋转数组的最小数字#
旋转数组的最小数字
http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
class Solution:
def minNumberInRotateArray(self, rotateArray):
left = 0
right = len(rotateArray) - 1
mid = 0
while (rotateArray[right] <= rotateArray[left]):
mid = int((left + right) / 2)
if right - left == 1:
return rotateArray[right]
# 这种情况下就不知道缩小哪边了,只能遍历了
# [1, 0, 1, 1, 1]
# [1, 1, 1, 0, 1]
if rotateArray[left] == rotateArray[mid] and rotateArray[mid] == rotateArray[right]:
min_num = rotateArray[left]
for item in rotateArray[left:right]:
min_num = min(item, min_num)
return min_num
# 当前mid处于第一个递增序列
if rotateArray[mid] >= rotateArray[left]:
left = mid
if rotateArray[mid] <= rotateArray[right]:
right = mid
return rotateArray[left]