《剑指Offer》之“旋转数组的最小数字”
2018-03-23 00:29
459 查看
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。代码实现
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { if (rotateArray.empty()) return 0; unsigned int first = 0, last = rotateArray.size() - 1; unsigned int mid = first; while (rotateArray[first] >= rotateArray[last]) { if (last - first == 1) { mid = last; break; } mid = first + (last - first) / 2; if (rotateArray[first] == rotateArray[mid] && rotateArray[mid] == rotateArray[last]) return minNumberInRotateArray(rotateArray, first, last); if (rotateArray[first] <= rotateArray[mid]) first = mid; else if (rotateArray[mid] <= rotateArray[last]) last = mid; } return rotateArray[mid]; } int minNumberInRotateArray(vector<int> &rotateArray, int first, int last) { int minVal = rotateArray[first]; for (int i = first; i <= last; ++i) minVal = min(minVal, rotateArray[i]); return minVal; } };
//题意再理解 class Solution { public: int minNumberInRotateArray(vector<int> rotateArray){ int size =rotateArray.size(); if(size == 0){ return 0 ; } int min = 0; for(int i=0 ;(i+1)<size;i++){ if(rotateArray[i] > rotateArray[i+1]){ min = i+1; } } return rotateArray[min]; } };
//最强解法,但时间复杂度并没有降低 //sort(*a,*b)函数是,对一个字符串从前到后重新 class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { sort(rotateArray.begin(),rotateArray.end()); return rotateArray[0]; } };
//该部分解释参考文章:❶ //C++STL中vector容器 begin()与end()函数、front()与back()的用法 //一、begin函数/end函数返回一个当前vector容器中起始元素的迭代器。 // 且end指向的是最后一个元素的下一个位置,所以访问最后一个元素 //二、front函数和back函数返回当前vector容器中起始元素的引用。 //#include <iostream> #include <vector> using namespace std; int main() { vector<char> v1; vector<char>::iterator iter1; vector<char>::iterator iter2; v1.push_back('m'); v1.push_back('n'); v1.push_back('o'); v1.push_back('p'); cout << "v1.front() = " << v1.front() << endl; cout << "v1.back() = " << v1.back() << endl; iter1 = v1.begin(); cout << *iter1 << endl; iter2 = v1.end()-1; //注意v1.end()指向的是最后一个元素的下一个位置,所以访问最后一个元素 //的正确操作为:v1.end() - 1; cout << *iter2 << endl; return 0; } //该部分解释参考文章:❷ //对sort的使用 //可以用 less 和greater方法来配合进行处理,升序和降序。 int a[10]={5,6,7,8,9,0,1,2,3,4}; vector <int> v(a, a+10); sort(v.begin(), v.end());//默认升序 sort(v.begin(), v.end(),less<int>());//升 sort(v.begin(), v.end(),greater<int>());//降
❶那年聪聪:https://blog.csdn.net/duan19920101/article/details/51679517
❷zhjiemm: https://zhidao.baidu.com/question/488352636.html
相关文章推荐
- 剑指Offer------旋转数组的最小数字
- 剑指Offer_11_旋转数组的最小数字
- 剑指offer(C++)——旋转数组的最小数字
- 剑指offer__03__旋转数组的最小数字
- 《剑指Offer》查找和排序——旋转数组中的最小数字
- 《剑指offer》旋转数组的最小数字
- 《剑指Offer》面试题:旋转数组的最小数字
- 剑指Offer(第二版)面试题11:旋转数组的最小数字
- 剑指offer—旋转数组的最小数字
- 剑指offer-题8:旋转数组的最小数字
- 《剑指Offer》 旋转数组的最小数字
- 剑指Offer系列---(11)旋转数组的最小数字
- 剑指offer-面试题8.旋转数组的最小数字
- 《剑指offer》--011--旋转数组中的最小数字
- 《剑指offer》牛客网java题解-旋转数组的最小数字
- (剑指offer)旋转数组的最小数字
- 旋转数组的最小数字(剑指offer)
- 《剑指Offer》面试题八之旋转数组的最小数字
- 剑指offer之面试题8旋转数组的最小数字
- 剑指offer——旋转数组的最小数字______