前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >牛客网 旋转数组的最小值

牛客网 旋转数组的最小值

作者头像
发布2019-02-25 15:36:15
6180
发布2019-02-25 15:36:15
举报

题目:旋转数组的最小值

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

第一种思路是提交了的代码中的最上边的几种解法

第二种思路来自于一本书,总体是采用的二分的思想,考虑二分之后最小值可能所处的位置

class Solution:
    def minNumberInRotateArray(self, rotateArray):
        # write code here
        pre = -7e20
        for num in rotateArray:
            if num < pre:
                return num
            pre = num

        if len(rotateArray) == 0:
            return 0
        return rotateArray[0]

    # 二分的思路
    def minNumberInRotateArray2(self, rotateArray):
        low=0
        high=len(rotateArray)-1
        mid=0
        while low<high:
            if low==high-1:
                break
            if rotateArray[low]<rotateArray[high]:
                return rotateArray[low]
            mid=(low+high)//2
            if(rotateArray[low]>rotateArray[mid]):
                high=mid
                continue
            if rotateArray[mid]>rotateArray[high]:
                low=mid
                continue
            while low<mid:
                if rotateArray[low]==rotateArray[mid]:
                    low+=1
                elif rotateArray[low]<rotateArray[mid]:
                    return rotateArray[low]
                else:
                    high=mid
                    break
        return min(rotateArray[low],rotateArray[high])
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年09月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:旋转数组的最小值
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档