【剑指offer】旋转数组求最小值-python实现

旋转数组的最小数字

http://www.nowcoder.com/questionTerminal/9f3231a991af4f55b95579b44b7a01ba

思路:
非递减数组进行旋转,采用变形的二分法,通过判断训练处于递增或是递减序列,看寻找最小值。分为三种情况:
当low<high时,直接返回index为low的数组值
1.处于递增:low上移;
2.处于递减:high下移;
3.不增不减,low++进行范围缩小
代码:

# -*- coding:utf-8 -*-
class Solution:
    def minNumberInRotateArray(self, rotateArray):
        # write code here
        if len(rotateArray)==0:
            return 0
        low=0
        high=len(rotateArray)-1
        mid=0
        while low<high:
            mid=low+(high-low)/2
            if rotateArray[low]<rotateArray[high]:
                return rotateArray[low]
            elif rotateArray[mid]>rotateArray[low]:
                low+=1
            elif rotateArray[high]>rotateArray[mid]:
                high=mid
            else:
                low+=1
        return rotateArray[low]
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务