Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

二分查找就行了。

 class Solution {
public:
int findMin(vector<int> &arr) {
if (arr.empty()) return -;
int l = , h = arr.size() - , m, min = INT_MAX;
while (l <= h) {
m = l + (h - l) / ;
if (arr[m] >= arr[l]) {
if (min > arr[l]) min = arr[l];
l = m + ;
} else {
if (arr[m] < min) min = arr[m];
h = m - ;
}
}
return min;
}
};

Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?
Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

The array may contain duplicates.

 class Solution {
public:
int findMin(vector<int> &arr) {
if (arr.empty()) return -;
int l = , h = arr.size() - , m, min = INT_MAX;
while (l <= h) {
m = l + (h - l) / ;
if (arr[m] > arr[l]) {
if (min > arr[l]) min = arr[l];
l = m + ;
} else if (arr[m] < arr[l]) {
if (arr[m] < min) min = arr[m];
h = m - ;
} else {
if (arr[m] < min) min = arr[m];
l++;
}
}
return min;
}
};

Leetcode | Find Minimum in Rotated Sorted Array I && II的更多相关文章

  1. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  2. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  5. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  6. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  7. LeetCode——Find Minimum in Rotated Sorted Array II

    Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...

  8. Find Minimum in Rotated Sorted Array I & II

    Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...

  9. Find Minimum in Rotated Sorted Array I&&II——二分查找的变形

    Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...

随机推荐

  1. hdu1213 并查集(不压缩)

    确定比赛名次 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名 ...

  2. NumPy 学习(1): ndarrays

    Numpy 是Numerical Python的简写,用来进行高性能的科学计算以及数据分析的基础包.它是一些高级工具(pandas)的基础.它主要提供以下几个功能: (1). ndarray:计算快, ...

  3. MFC 打开文件夹 调用其他程序 打开文件

    ShellExecute(NULL,TEXT("OPEN"),要打开的文件的路径,NULL,NULL,SW_SHOWNORMAL); ShellExecute(NULL, &quo ...

  4. figure元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. POJ3686 The Windy's(最小费用最大流)

    题目大概说要用m个工厂生产n个玩具,第i个玩具在第j个工厂生产要Zij的时间,一个工厂同一时间只能生成一个玩具,问最少的用时. 这题建的图不是很直观.. 源点向玩具连容量1费用0的边 将每个工厂拆成n ...

  6. CSS中文字体对照表

    http://hotoo.googlecode.com/svn/trunk/labs/css/css-fonts.html CSS中文字体对照表 css字体名可以使用2种Unicode格式,以“微软雅 ...

  7. git 回滚

    git reset --hard HEAD~10 可以通过上面的命令会退到最初的版本查看源代码, git reset --hard 4aa9a32d1625997ef5b28463ccde78d711 ...

  8. gulp顺序执行任务

    gulp的任务的执行是异步的. 所以,当我写完一系列的任务,准备一股脑地执行. # gulp.task('prod', ['clean', 'compass', 'image', 'style', ' ...

  9. ACM 无线网络覆盖

    无线网络覆盖 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们的乐乐同学对于网络可算得上是情有独钟,他有一个计划,那就是用无线网覆盖郑州大学. 现在学校给了他一个 ...

  10. ACM: 强化训练-百度之星-Problem C-字典树

    Problem C Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Descript ...