标签:pre cin indent 元素 out min amp space 旋转数组
#include <iostream> #include <algorithm> #include "string.h" #include "stdio.h" #include <vector> #include <deque> #include <stack> #include<map> #include<utility> #include "math.h" using namespace std; class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { if(rotateArray.size() == 0) return 0; int left = 0; int right = rotateArray.size()-1; return minNumber(rotateArray,left,right); } int minNumber(vector<int> arr,int left,int right) { int mid = left+(right-left)/2; if(right-left <= 1) return arr[left]<=arr[right]?arr[left]:arr[right]; if(arr[left]<arr[right]) return arr[left]; if(arr[left]>=arr[right]) { if(arr[mid]>arr[right])//4 5 6 7 8 9 1 2 3 { return minNumber(arr,mid+1,right); } if(arr[left]>arr[mid])//7 8 9 1 2 3 4 5 6 { return minNumber(arr,left,mid);//mid也可能是最小的 } } if(arr[left]==arr[right]&& arr[left]==arr[mid]) { for(int i=0;i<arr.size();i++) { if(arr[i]<arr[left]) { return arr[i]; } } return arr[left]; } return -1; } }; int main() { vector<int> arr; arr.push_back(2); arr.push_back(1); arr.push_back(2); arr.push_back(2); arr.push_back(2); Solution solution; int res = solution.minNumberInRotateArray(arr); cout<<res<<endl; }
标签:pre cin indent 元素 out min amp space 旋转数组
原文地址:http://www.cnblogs.com/omelet/p/6648809.html