旋转数组的最小值

 

 解题思路:

用二分查找,找中间的那个数字和左右两边的数字进行比较。

右边的值大于中值,说明最小值在左边# -*- coding:utf-8 -*-

class Solution:
    def minNumberInRotateArray(self, rotateArray):
        if not rotateArray:
            return None
        left=0
        right=len(rotateArray)-1
        while left<=right:
            mid=(left+right)>>1
#右移表示整除等价于mid=(left+right)//2
#110=5->11=2
#1100=12->110=6
if rotateArray[mid]<rotateArray[mid-1]:
return rotateArray[mid]
            elif rotateArray[mid]<rotateArray[right]:
                right=mid-1
            else:
                left=mid+1
        return 0
                
        # write code here
# -*- coding:utf-8 -*-
#另外解法,简单粗暴。
class Solution: def minNumberInRotateArray(self, rotateArray): minNum=0 for i in range(0,len(rotateArray)): minNum=minNum if minNum<rotateArray[i] and minNum !=0 else rotateArray[i] return minNum # write code here

 

posted @ 2021-04-01 16:55  努力中的小菜鸟  阅读(89)  评论(0)    收藏  举报