【剑指offer-解题系列(6)】旋转数组中的最小数
2017-05-17 10:29
239 查看
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
分析
折半查找,分为以下几种判断:1、开始位置的数>结束位置的数,说明是递增,返回开头数字。
2、开始位置的数<=结束位置的数,
(1) 中间数 < 开头数, 向左查找
(2) 中间数 >=
开头数,说明起点在右边,向右查找
代码实现
int minNumberInRotateArray(vector<int> rotateArray) { if(rotateArray.size()<=0) return 0; int left = 0,right = rotateArray.size()-1 ,mid = 0;while(left<right){ mid = (left+right )/2; if(rotateArray[left] < rotateArray[right]) return rotateArray[left]; else{ if(rotateArray[left] > rotateArray[mid] ) right = mid; else if(rotateArray[left] <= rotateArray[mid]) left = mid+1; } } return rotateArray[left];}
相关文章推荐
- 【剑指offer-解题系列(33)】把数组排成最小的数
- 剑指offer:08 旋转数组的最小数字
- 剑指offer-08旋转数组的最小数字
- 【剑指Offer学习】【面试题8 : 旋转数组的最小数字】
- leetcode之Search in Rotated Sorted Array,剑指offer之旋转数组的最小数字
- 【九度OJ1386】|【剑指offer8】旋转数组的最小数字
- 剑指offer-8 旋转数组的最小数字
- 【剑指offer】面试题八:旋转数组的最小数字
- 剑指offer面八_旋转数组的最小数字
- 【剑指offer】 面试题8: 旋转数组的最小数字
- 【剑指Offer面试编程题】题目1386:旋转数组的最小数字--九度OJ
- 【剑指Offer面试编程题】题目1386:旋转数组的最小数字--九度OJ
- 【剑指offer】旋转数组的最小值
- 剑指offer--旋转数组的最小数字
- 剑指offer 面试题8 输出旋转数组最小值
- 剑指 offer set 3 旋转数组的最小数字
- 剑指OFFER之旋转数组的最小数字(九度OJ1386)
- 剑指offer--旋转数组的最小数字
- 剑指Offer之旋转数组的最小数字
- 剑指offer 8 - 旋转数组的最小数字