zoukankan      html  css  js  c++  java
  • 剑指offer旋转数组的最小数字python

    题目描述

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

    思路

    旋转后的数组先递增,然后突然断层,让后又递增,所以,只要找到数组突然变小的那个数字即可。

    代码

    # -*- coding:utf-8 -*-
    class Solution:
        def minNumberInRotateArray(self, rotateArray):
            if rotateArray is None:
                return None
            temp = rotateArray[0]
            for i in range(len(rotateArray) - 1):
                if rotateArray[i] > rotateArray[i+1]:
                    temp = rotateArray[i+1]
                    break
            return temp
    

      

  • 相关阅读:
    ssh 远程命令
    POJ 2287
    POJ 2376
    hihoCoder1488
    POJ1854
    HDU 5510
    HDU 4352
    CodeForces 55D
    HDU 1517
    CodeForces 1200F
  • 原文地址:https://www.cnblogs.com/wangzhihang/p/11781065.html
Copyright © 2011-2022 走看看