[剑指Offer] 6.旋转数组的最小数字(二分法)
来源:互联网 发布:以色列人工智能武器 编辑:程序博客网 时间:2024/05/29 14:30
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
【思路1】直接调用快排~~~
1 class Solution {2 public:3 int minNumberInRotateArray(vector<int> rotateArray) {4 sort(rotateArray.begin(),rotateArray.end());5 return rotateArray[0];6 }7 };
【思路2】使用二分法
。。。像素太渣了,凑合看吧
1 class Solution { 2 public: 3 int minNumberInRotateArray(vector<int> rotateArray) { 4 int low = 0,high = rotateArray.size() - 1,mid; 5 while(low < high){ 6 mid = low + ( high - low ) / 2; 7 if(rotateArray[mid] > rotateArray[high]) 8 low = mid + 1; 9 else if(rotateArray[mid] == rotateArray[high])10 high = high - 1;11 else 12 high = mid;13 }14 return rotateArray[low];15 }16 };
阅读全文
0 0
- [剑指Offer] 6.旋转数组的最小数字(二分法)
- 剑指offer——旋转数组的最小数字(二分法思路)
- 剑指Offer的旋转数组的最小数字问题是要二分法解决
- 【剑指offer】旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指 offer:旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 剑指Offer 旋转数组的最小数字
- 剑指offer|旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 程序包org.apache.http不存在,解决方式
- [剑指Offer] 3.从尾到头打印链表
- [剑指Offer] 4.重建二叉树
- [剑指Offer] 5.用两个栈实现队列
- linux 验证码无法显示
- [剑指Offer] 6.旋转数组的最小数字(二分法)
- [剑指Offer] 7.斐波那契数列
- LINUX第一章作业
- [剑指Offer] 8.跳台阶
- tomcat的安装与配置
- [剑指Offer] 9.变态跳台阶
- termios详解
- [剑指Offer] 10.矩形覆盖
- [剑指Offer] 11.二进制中1的个数