Leetcode Find Minimum in Rotated Sorted Array

题目大意:

对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数。注意,K有可能是0,也就是没有翻转。

毫无疑问,遍历一次肯定能够找到,但这样时间复杂度是O(n)。假设你在面试的时候遇到这种问题,你这样回答面试官肯定不会惬意的。我们接下来讨论有没有什么更快的方法。O(nlogn)??

我还是把O(N)的代码贴出来,不知道为什么leetcode上竟然不超时。

//O(n)
class Solution {
public:
int findMin(vector<int> &num) {
int minval = 0x3f3f3f3f;
int len = num.size();
for (int i = 0; i < len; i++) {
minval = min(minval, num[i]);
}
return minval;
}
};

既然数字開始是有序的,我们第一个应该想到的方法就是二分。其实就是能够二分。 假设进行的翻转,肯定会变成两部分有序数组。第一部分的不论什么一个数都大于第二部分的,第二部分中最后一个数肯定是最大的。这样二分的推断条件就是,仅仅要mid位置的值大于最后一个数。肯定能确定mid在第一部分中,然后往右二分就可以,反之往左。

我本人写了两次代码,第一份非常不尽人意,首先对没有翻转的情况做了特判。然后一直遇到二分边界的问题。仅仅要二分两个数的时候就会死循环,索性就加了特判,两个数的时候直接输出最小的一个,代码冗长混乱。思路也不严谨,先贴出来做个反例。

//O(nlogn) bad
class Solution {
public:
int findMin(vector<int> &num) {
int len = num.size();
if (num[0] <= num[len-1])
return num[0];
int l = 0, r = len-1;
while (l < r) {
int mid = (l+r)>>1;
if (num[mid] > num[l]) {
if (num[mid] > num[0])
l = mid+1;
if (num[mid] < num[len-1])
r = mid;
}
else if (num[mid] < num[r])
r = mid;
else
return min(num[l], num[r]);
}
return num[l];
}
};

接下来就是改动后的精简代码,事实上不须要特判,又一次写了二分的推断条件,然后代码瞬间短了好多,也易于理解。

//O(nlogn) good
class Solution {
public:
int findMin(vector<int> &num) {
int len = num.size();
int l = 0, r = len-1;
while (l < r) {
int mid = (l+r)>>1;
if (num[mid] > num[r])
l = mid+1;
else
r = mid;
}
return num[l];
}
};

Leetcode Find Minimum in Rotated Sorted Array 题解的更多相关文章

  1. LeetCode Find Minimum in Rotated Sorted Array II

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

  2. LeetCode Find Minimum in Rotated Sorted Array

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

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

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

  4. [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 ...

  5. 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 migh ...

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

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

  7. Leetcode Find Minimum in Rotated Sorted Array I and II

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

  8. LeetCode——Find Minimum in Rotated Sorted Array

    Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...

  9. [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 ...

随机推荐

  1. [2018湖南省队集训] 6.24 T1 marshland

    题面在这里! 一开始感觉像一个类似二分图的最小割,于是成功跑偏2333333 很容易发现一个关键性质,'L'的两个角落在的偶数格 的行(或者列)的奇偶性一定不同.... 于是我们再把偶数格按照行(或者 ...

  2. BZOJ 2152 聪聪可可(点分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2152 [题目大意] 给出一棵树,问任取两点之间距离为3的倍数的概率是多少 [题解] 树 ...

  3. Problem B: 查找某一个数

    #include<stdio.h> int main(void) { ]; int i; char ch='n'; while(scanf("%d %d",&x ...

  4. cookie和localStorage、sessionStorage的区别

    先来讲讲localStorage吧,我最初接触localStorage,是听一个同学说他在做项目的过程中用到过这个.但是我自己也用到过的,就是在学习React的时候,在做一个小demo,这个demo简 ...

  5. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...

  6. 使用织梦开源的分词算法库编写的YII获取分词扩展

    在编辑文章中,很多时候都需要自动根据文章内容获取关键字的功能,因此,本文主要是说明如何在yii中使用织梦开源的分词算法编写一个独立的扩展,可以在不同的模块中使用,步骤如下: 1 到这里下载其他朋友整理 ...

  7. Mac下的MySQL修改默认连接字符集

    进入命令行执行以下命令: sudo vim /etc/my.cnf [client] default-character-set=utf8 [mysqld] character-set-server= ...

  8. Sql实现行列转换

    从MS Sql Server 2005微软就推出了pivot和unpivot实现行列转换,这极大的方便了我们存储数据和呈现数据.今天就对这两个关键字进行分析,结合实例讲解如何存储数据,如何呈现数据. ...

  9. <摘录>简述configure、pkg-config、pkg_config_path三者的关系

    一.什么是configure 源码安装过程中大多会用到configure这个程序,一般的configure都是一个script,执行时可以传入必要参数告知配置项目. configure程序它会根据传入 ...

  10. 1:MUI选择器组件抛出“n.getSelectedItem is not a function”异常的解决办法 2:mui三级联动 3:移动端关闭虚拟键盘

    1:如下图 问题:引用了mui的地址选择的三级联动的应用在h5上的组件 百度发现别人思路对 Array 原型链方法扩充时,会抛出这个异常. 修改方法: mui.poppicker.js 第 112 行 ...