第一直觉,暴力搜索
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if rotateArray == None:
return 0
p1 = 0
p2 = 1
while rotateArray[p1] <= rotateArray[p2]:
p2 = p2 + 1
return rotateArray[p2]
复杂度O(N)
剑指offer书:
二分查找思想:
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if rotateArray == None :
return 0
p1 = 0
l = len(rotateArray)
p2 = l-1
while p1 != p2-1:
p = p1 + int((p2-p1)/2)
if rotateArray[l-1] > rotateArray[p]:
p2 = p
else:
p1 = p
return rotateArray[p2]
复杂度O(logn)
书上的例子[1,0,1,1,1]和[1,1,1,0,1],第二种方法不适用,第一种暴力可解,这种特殊例子应该也有减小计算量的方法吧