旋转数组的最小数子
作者:
而立之年的技术控 | 来源:发表于
2019-12-20 19:05 被阅读0次
微信图片_20191220190256.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
本文标题:旋转数组的最小数子
本文链接:https://www.haomeiwen.com/subject/umwinctx.html
网友评论