【剑指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]
全部评论

相关推荐

09-28 09:18
吉首大学 Java
离上岸不远了的牛油很...:同27,你写的专业技能那些是真熟练了吗,我感觉稍微问深一点我都要🐔
你找实习最大的坎坷是什么
点赞 评论 收藏
分享
迷茫的大四🐶:都收获五个了,兄弟那还说啥,不用改了,去玩吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务