[6] to prove safety Offer the smallest number of rotation of the array to achieve python

Title Description

The beginning of an array of several elements moved to the end of the array, the array we call rotation.
A non-descending order of the input array of a rotation of the output rotary smallest element array.
For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum.
NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.

Binary search

class Solution:
    def minNumberInRotateArray(self, rotateArray):
        # write code here
        length = len(rotateArray)
        if length == 0:
           return 0
        elif length == 1:
            return rotateArray[0]
        else:
            left = 0
            right = length - 1
            mid = 0
            while rotateArray[left] >= rotateArray[right]:
                if right - left == 1:
                    mid = right
                    break
                mid = left + (right - left) // 2
                if rotateArray[mid] >= rotateArray[left]:
                    left = mid
                if rotateArray[mid] <= rotateArray[right]:
                    right = mid

            return rotateArray[mid]
Published 99 original articles · won praise 6 · views 3995

Guess you like

Origin blog.csdn.net/weixin_42247922/article/details/103915946