lintcode 160 寻找旋转排序数组中的最小值 II
来源:互联网 发布:iphone mac地址伪装 编辑:程序博客网 时间:2024/04/27 08:57
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。
你需要找到其中最小的元素。
数组中可能存在重复的元素。
样例
给出[4,4,5,6,7,0,1,2] 返回 0
#include <iostream>#include <vector>using namespace std;class Solution {public:/*** @param num: the rotated sorted array* @return: the minimum number in the array*/int findMin(vector<int> &num) {// write your code hereint len = num.size();if (len<1){return 0;}int mid = 0;int low = 0;int height = len-1;while (num[low]>=num[height]){if (height - low == 1){return num[height];}mid = (low + height) / 2;//如果low height mid的值相同,只能顺序查找if (num[low] == num[mid] && num[mid] == num[height] && num[height] == num[low]){return MinInOrder(num, low, height);}if (num[mid]>=num[low]){low = mid;}else if (num[height] >= num[mid]){height = mid;}}return num[mid];}int MinInOrder(vector<int> array, int low, int height){int result = array[low];for (int i = low + 1; i <= height;i++){if (result>array[i]){result = array[i];}}return result;}};int main(){int a[] = { 4, 4, 5, 6, 7, 0, 1, 2 };int len = sizeof(a) / sizeof(a[0]);vector<int> array(a, a + len);Solution s;cout << s.findMin(array);return 0;}
1 0
- lintcode 160 寻找旋转排序数组中的最小值 II
- LintCode-寻找旋转排序数组中的最小值 II
- Lintcode 寻找旋转排序数组中的最小值 II
- lintcode --寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II -LintCode
- LintCode-分治-寻找旋转排序数组中的最小值 II
- LintCode-寻找旋转排序数组中的最小值
- LintCode 寻找旋转排序数组中的最小值
- LintCode-寻找旋转排序数组中的最小值
- lintcode 寻找旋转排序数组中的最小值
- lintcode,寻找旋转排序数组中的最小值
- lintcode,寻找旋转排序数组中的最小值
- LintCode : 寻找旋转排序数组中的最小值
- lintcode--寻找旋转排序数组中的最小值
- 寻找旋转排序数组中的最小值-LintCode
- 寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II
- 寻找旋转排序数组中的最小值 II
- ....are only available on JDK 1.5 and higher
- wpa_supplicant移植安装
- MySQL语句大全:创建、授权、查询、修改等
- socket03---小变形,点对点通信聊天
- JZOJ 4465【GDOI2016模拟4.22】飞机调度
- lintcode 160 寻找旋转排序数组中的最小值 II
- 剑指offer(49):约瑟夫环问题(圆圈中最后剩下的数字)
- 《linux高性能服务器编程》学习笔记(三)之I/O复用,信号,定时器
- [HDU 4587] TWO NODES (Tarjan+割点)
- 340 - Master-Mind Hints
- 读书笔记---戒掉吧,拖延症
- [AutoVue开发手册]第一篇——自定义Applet脚本
- 1005. Spell It Right (20)
- html学习