1. 程式人生 > >旋轉陣列的最小數字(無重複數字)

旋轉陣列的最小數字(無重複數字)

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        if(rotateArray.size()==1)
        {
            return rotateArray[0];
        }
        int left=0, right = rotateArray.size()-1;
        if(rotateArray[left]<rotateArray[right])
        {
            return
rotateArray[left]; } while(left<=right) { int mid = left + (right - left) / 2; if(rotateArray[mid]<rotateArray[right]) { right = mid; } else { left = mid + 1; }
} return rotateArray[right]; } };
# -*- coding:utf-8 -*-
class Solution:
    def minNumberInRotateArray(self, rotateArray):
        # write code here
        if len(rotateArray)==1:
            return rotateArray[0]
        left, right = 0, len(rotateArray)-1
        if rotateArray[
left] < rotateArray[right]: return rotateArray[left] while left<=right: mid = left + (right - left)/2 if rotateArray[mid-1] > rotateArray[mid]: return rotateArray[mid] if rotateArray[mid] > rotateArray[mid+1]: return rotateArray[mid+1] if rotateArray[mid] < rotateArray[right]: right = mid - 1 else: left = mid + 1