剑指offer8旋转数组的最小数字
来源:互联网 发布:三菱触摸屏编程软件 编辑:程序博客网 时间:2024/06/11 03:22
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{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;
int size=rotateArray.size();
int left=0,right=size-1,mid=0;
while(rotateArray[left]>=rotateArray[right])
{
if(right-left==1)
{
mid=right;
break;
}
int mid=left+(right-left)/2;
if(rotateArray[left]==rotateArray[right]&&rotateArray[left]==rotateArray[mid])//数组11101
return MinOrder(rotateArray,left,right);
if(rotateArray[mid]>=rotateArray[left])
left=mid;
else
right=mid;
}
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;
}
};
阅读全文
0 0
- 剑指Offer8:旋转数组的最小数字
- 剑指offer8:旋转数组的最小数字
- 剑指offer8旋转数组的最小数字
- 剑指Offer8 旋转数据的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指 offer:旋转数组的最小数字
- 剑指offer6 旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 剑指Offer 旋转数组的最小数字
- android studio和github进行关联和如何向README.md文件中添加图片说明
- C++ 的忠告
- Github--github结构+gitbub本地文件上传
- python repr、str、反引号值转化为字符串区别
- 福利来了【干货】融云音视频开发 iOS CallKit 开发指南
- 剑指offer8旋转数组的最小数字
- 动态规划之01背包,完全背包,多重背包模板
- RXJava入门01
- 导航栏翻转效果
- 加载本地pdf文件,关于so文件的坑
- 时间戳转为C#格式时间 (互转)
- 【LeetCode】Length of Last Word 解题报告
- Splay模板
- 初识html