解题思路
一开始是一个有序的数组,旋转之后的两部分各自是有序的。
看见有序,肯定就是二分查找了,算法比较简单,不多说,值得一提的是,不要拘泥于递归,要会循环写法。
python 代码
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
length = len(rotateArray)
if length == 0:
return 0
elif length == 1:
return rotateArray[0]
else:
left = 0
right = length - 1
mid = 0
while rotateArray[left] >= rotateArray[right]:
if right - left == 1:
mid = right
break
mid = left + (right - left) // 2
if rotateArray[mid] >= rotateArray[left]:
left = mid
if rotateArray[mid] <= rotateArray[right]:
right = mid
return rotateArray[mid]
s = Solution()
result = s.minNumberInRotateArray([3,4,5,1,2])
print(result)