zoukankan      html  css  js  c++  java
  • 旋转数组的最小数字-顺序查找

    题目描述

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

    解答

    如果数组第一个数比最后一个数小,则表示该数组没有进行旋转,是有序的,第一个元素就是最小的。

    如果数组第一个数比最后一个数大,则表示该数组进行了旋转,从倒叙查,当查到一个元素大于第一个元素的值,则这个元素的后一个元素就是最小的。

    # coding:utf-8
    
    class Solution:
        def minNumberInRotateArray(self, rotateArray):
            # write code here
            if len(rotateArray) == 0:
                return 0
            low = 0
            high = len(rotateArray) - 1
            if rotateArray[low] > rotateArray[high]:
                while rotateArray[low] > rotateArray[high]:
                    high -= 1
                return rotateArray[high+1]
            if rotateArray[low] == rotateArray[high]:
                return rotateArray[low]
            if rotateArray[low] < rotateArray[high]:
                return rotateArray[low]
    
    
    ret = Solution().minNumberInRotateArray([3,4,5,1,2])
    
    print ret

    结束!

  • 相关阅读:
    将VSCode添加至右键菜单(Windows下)
    VSCode 快捷键
    dijkstra 优先队列最短路模板
    运营苹果手机“盗改销”、色情网站的黑产组织追踪
    Wireshark 设置显示端口号
    IDA_API_Help
    IDA配置
    windbg vmware配置
    !heap命令问题 Windbg
    落户
  • 原文地址:https://www.cnblogs.com/aaronthon/p/13729601.html
Copyright © 2011-2022 走看看