旋转数组的最小数字
来源:互联网 发布:蜂群算法代码 编辑:程序博客网 时间:2024/06/06 04:50
- 题目:
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。 - 自己的代码
class Solution {public: int minNumberInRotateArray(vector<int> rotateArray) { int k; if(rotateArray.empty())return 0; for(int i=0;i<rotateArray.size()-1;i++){ if(rotateArray[i]>rotateArray[i+1]){ k=rotateArray[i+1]; break; } } return k; }};
- 剑指Offer上的代码
class Solution {public: int MinInOrder(vector<int> numbers, int index1, int index2){ int result = numbers[index1]; for (int i = index1 + 1; i <= index2; i++){ if (result > numbers[i]) result = numbers[i]; } return result;} int minNumberInRotateArray(vector<int> rotateArray) { //if (rotateArray.empty()) //throw new std::exception("Invalid parameters"); int index1 = 0; int index2 = rotateArray.size() - 1; int indexMid = index1; while (rotateArray[index1] >= rotateArray[index2]){ if (index2 - index1 == 1){ indexMid = index2; break; } indexMid = (index1 + index2) / 2; if (rotateArray[index1] == rotateArray[index2] && rotateArray[indexMid] == rotateArray[index1]){ return MinInOrder(rotateArray, index1, index2); } if (rotateArray[indexMid] >= rotateArray[index1]) index1 = indexMid; else if (rotateArray[indexMid] <= rotateArray[index2]) index2 = indexMid; } return rotateArray[indexMid]; }};
0 0
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- LoadRunner总结(3)-LoadRunner发送get和post请求
- 调用系统拍照 -- 踩过的坑
- SUSE 11 SP2下给普通用户加上sudo命令权限
- linphone-TunnelConfigImpl文件对应的JNI层文件分析
- (三)表的概念
- 旋转数组的最小数字
- 简单的贪心
- Sort(啊哈算法)
- Android开发之仿QQ侧滑删除实现(二)
- hbase 1.1.4增删查改demo
- cdoj 1355 郭大侠与“有何贵干?”
- Tomcat8.x 《设计模式》Facade设计模式的使用->getServletContext()
- NOIP 2006 - 提高组 金明的预算 动态规划(DP)
- linux kernel启动流程