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. 【BZOJ 1998】 1998: [Hnoi2010]Fsk物品调度(双向链表+并查集+置换)

    1998: [Hnoi2010]Fsk物品调度 Description 现在找工作不容易,Lostmonkey费了好大劲才得到fsk公司基层流水线操作员的职位.流水线上有n个位置,从0到n-1依次编号 ...

  2. 最优贸易 NOIP 2009 提高组 第三题

    题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...

  3. [BZOJ2726][SDOI2012]任务安排(DP+凸壳二分)

    2726: [SDOI2012]任务安排 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1580  Solved: 466[Submit][Statu ...

  4. 【二分】Jessica's Reading Problem

    [POJ3320]Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1309 ...

  5. python基础之闭包函数与装饰器

    闭包函数: 什么是闭包函数: 闭指的是定义在一个函数内部 包指的是该函数包含对外部作用域(非全局作用域)名字的引用 def counter(): n=0 def incr(): nonlocal n ...

  6. (原创)Stanford Machine Learning (by Andrew NG) --- (week 8) Clustering & Dimensionality Reduction

    本周主要介绍了聚类算法和特征降维方法,聚类算法包括K-means的相关概念.优化目标.聚类中心等内容:特征降维包括降维的缘由.算法描述.压缩重建等内容.coursera上面Andrew NG的Mach ...

  7. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

  8. MYSQL复习笔记12-视图

    Date: 20140223Auth: Jin参考:http://blog.sina.com.cn/s/blog_436732df0100e768.html 一.介绍1.概念视图是从一个或几个基本表( ...

  9. hdu1428漫步校园

    #include <queue> #include <iostream> #include <algorithm> #include <cstring> ...

  10. 微信小程序,开发者工具更新以后wxss编译错误

    出现上述错误,解决方法如下: 1.在控制台输入openVendor() : 2.清除里面的wcsc wcsc.exe 3.重启开发者工具 搞定!