剑指offer 旋转数组的最小数字
2018-02-28 19:54
337 查看
class Solution { public: int minNumberInRotateArray(vector<int> rotateArray) { int size = rotateArray.size(); if(size == 0){ return 0; }//if int left = 0,right = size - 1; int mid = 0; // rotateArray[left] >= rotateArray[right] 旋转数组的左侧元素大于最右侧 while(rotateArray[left] >= rotateArray[right]){ // 判断是否达到循环终止条件 即两个元素相邻 if(right - left == 1){ mid = right; break; }//if mid = (right + left) / 2; // 左右侧相等且等于中间元素时 // 无法确定中间元素属于哪一个序列 // 只能顺序查找 if(rotateArray[left] == rotateArray[right] && rotateArray[left] == rotateArray[mid]){ return MinOrder(rotateArray,left,right); }//if // 中间元素位于前面的递增子数组 // 此时最小元素位于中间元素的后面 if(rotateArray[mid] >= rotateArray[left]){ left = mid; }//if // 中间元素位于后面的递增子数组 // 此时最小元素位于中间元素的前面 else{ right = mid; }//else }//while return rotateArray[mid]; } private: // 顺序寻找最小值 int MinOrder(vector<int> &num,int left,int right){ int result = num[left]; for(int i = left + 1;i < right;++i){ if(num[i] < result){ result = num[i]; }//if }//for return result; } };
相关文章推荐
- 剑指offer-08:旋转数组的最小数字
- [剑指offer]旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 剑指Offer的旋转数组的最小数字问题是要二分法解决
- 剑指offer-旋转数组的最小数字
- 剑指offer-算法题练习:part6 旋转数组的最小数字
- 剑指Offer面试题8旋转数组的最小数字(二分查找)附带快排和按年龄排序
- 剑指Offer之旋转数组中的最小数字(题8)
- 剑指offer——旋转数组的最小数字
- 剑指offer-旋转数组的最小数字-二分查找法
- 【剑指offer】旋转递增数组的最小数字
- 剑指offer 旋转数组的最小数字
- 【剑指Offer面试编程题】题目1386:旋转数组的最小数字--九度OJ
- 【剑指offer】面试题11:旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer题8_旋转数组的最小数字
- 牛客网剑指offer-旋转数组的最小数字
- 剑指offer题解C++【6】旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer面试题[8]-旋转数组的最小数字