jianzhi-offer-python icon indicating copy to clipboard operation
jianzhi-offer-python copied to clipboard

《6. 旋转数组的最小数字》存在问题

Open Qipccc opened this issue 6 years ago • 0 comments

在《剑指offer》书中提到的数组为 [1,0,1,1,1] 这种情况时,这里程序输出为1是不正确的,可以改为:

class Solution:
    def minNumberInRotateArray(self, rotateArray):
        # write code here
        if len(rotateArray) == 0:
            return 0
        front = 0
        rear = len(rotateArray) - 1
        minVal = rotateArray[0]
        
        if rotateArray[front] < rotateArray[rear]:
            return rotateArray[front]
        else:
            while (rear - front) > 1: 
                mid = (front + rear) // 2
                if rotateArray[front] == rotateArray[rear] == rotateArray[mid]:
                    for i in range(1, len(rotateArray)):
                        if rotateArray[i] < minVal:
                            minVal = rotateArray[i]
                            rear = i
                elif rotateArray[mid] >= rotateArray[front]:
                    front = mid
                elif rotateArray[mid] <= rotateArray[rear]:
                    rear = mid
            minVal = rotateArray[rear]
            return minVal


Qipccc avatar May 21 '19 05:05 Qipccc