题解 | #旋转数组的最小数字#
旋转数组的最小数字
http://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
# 两个注意点 当left right为相邻值时,最小的是right值
# 当right值和mid值相等时,无法判断最小值在哪个区间,该情况暴力求解 (10111,11101,20111)
# 其余情况 二分法:当right>mid时,最小值在左区间;当right<mid时,最小值在右区间
left = 0
right = len(rotateArray) - 1
while left <= right:
if left == right-1: #left right相邻 right最小值
return rotateArray[right]
mid = (left + right) // 2
if rotateArray[mid] < rotateArray[mid-1]: # mid<mid-1 直接返回
return rotateArray[mid]
if rotateArray[right] > rotateArray[mid]: #right>mid 左区间
right = mid
elif rotateArray[right] < rotateArray[mid]: #right<mid 右区间
left = mid
else: #right==mid 无法判定 暴力求解
tmp = rotateArray[0]
for i in range(len(rotateArray)):
if tmp > rotateArray[i]:
tmp = rotateArray[i]
return tmp