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

    旋转数组的最小数字

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

     1 # -*- coding:utf-8 -*-
     2 class Solution:
     3     def minNumber(self,rotateArray,l,h):
     4         for i in range(l,h):
     5             if rotateArray[i] > rotateArray[i+1]:
     6                 return rotateArray[i+1]
     7         return rotateArray[l]
     8             
     9     def minNumberInRotateArray(self, rotateArray):
    10         n = len(rotateArray)
    11         if n == 0:
    12             return 0
    13         l,h = 0,n-1
    14         while l<h:
    15             m = l + (h-l)//2
    16             if rotateArray[m] == rotateArray[l] and rotateArray[m] == rotateArray[h]:
    17                 return self.minNumber(rotateArray,l,h)
    18             elif rotateArray[m] < rotateArray[h]:
    19                 h = m
    20             else:
    21                 l = m+1
    22         return rotateArray[l]
    23         # write code here
  • 相关阅读:
    数据库
    linux
    linux
    python_函数进阶1
    python_函数初识
    python_文件操作
    python_基础数据类型补充
    python 小数据池,代码块总览
    python_基础数据类型二
    python_基础数据类型一
  • 原文地址:https://www.cnblogs.com/asenyang/p/11013058.html
Copyright © 2011-2022 走看看