标签:需要 vector ota space off end return else 题目
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 //例如12345 的旋转数组34512 这类的左侧第一个值大一等于右侧 由于每个子数组是排好序的,所以用二分查找logn 5 //例如01111 的旋转数组11101 10111这种第一个值=中间值=最后值需要特殊处理 只能遍历 n 6 class Solution { 7 public: 8 int minNumberInRotateArray(vector<int> rotateArray) { 9 int n = rotateArray.size(); 10 if(n==0) 11 return 0; 12 int index1 = 0; 13 int index2 = n-1; 14 int indexMid = index1; 15 while(rotateArray[index1]>=rotateArray[index2]) 16 { 17 if(index2-index1 == 1)//相差一个时,是查找截止条件 18 { 19 indexMid = index2; 20 break; 21 } 22 indexMid = (index1+index2)/2; 23 if(rotateArray[index1] == rotateArray[index2] && rotateArray[index1] == rotateArray[indexMid])//特殊处理 24 { 25 return MinInOrder(rotateArray,index1,index2); 26 } 27 if(rotateArray[indexMid]>=rotateArray[index1])//中间值大于左侧值,最小值在右侧 28 { 29 index1 = indexMid; 30 } 31 else if(rotateArray[indexMid] <=rotateArray[index2])//中间值小于右侧值最小值在左侧 32 { 33 index2 = indexMid; 34 } 35 } 36 return rotateArray[indexMid]; 37 38 } 39 int MinInOrder(vector<int> rotateArray,int index1,int index2) 40 { 41 int result = rotateArray[index1]; 42 for(int i=index1+1;i<=index2;i++) 43 { 44 if(rotateArray[i]<result) 45 { 46 result = rotateArray[i]; 47 } 48 } 49 return result; 50 } 51 }; 52 int main() 53 { 54 vector<int> a; 55 a.push_back(3); 56 a.push_back(4); 57 a.push_back(5); 58 a.push_back(1); 59 a.push_back(2); 60 Solution s; 61 int result = s.minNumberInRotateArray(a); 62 cout<<"the min of 12345 is"<<result<<endl; 63 64 vector<int> b; 65 b.push_back(1); 66 b.push_back(0); 67 b.push_back(1); 68 b.push_back(1); 69 b.push_back(1); 70 result = s.minNumberInRotateArray(b); 71 cout<<"the min of 01111 is"<<result<<endl; 72 return 0; 73 }
输出结果截图:
标签:需要 vector ota space off end return else 题目
原文地址:http://www.cnblogs.com/qqky/p/6811466.html