旋转数组的最小数子
2019-12-20 本文已影响0人
而立之年的技术控
![](https://img.haomeiwen.com/i13792534/4e4f686129ea1f9b.jpg)
##### 【解法一:遍历】####
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if len(rotateArray)==0:
return 0
if len(rotateArray) == 1:
return rotateArray[0]
else:
for index in range(1, len(rotateArray)):
if rotateArray[index] < rotateArray[index-1]:
return rotateArray[index]
##### 【解法一:二分法】####
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
left = 0
right = len(rotateArray) - 1
while left <= right:
mid = (left + right) >> 1
if rotateArray[mid] > rotateArray[mid+1]:
return rotateArray[mid+1]
if rotateArray[mid] > rotateArray[right]:
left = mid
else:
right = mid