Leetcode Find Minimum in Rotated Sorted Array 题解
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 题解的更多相关文章
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- 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 ...
- LeetCode——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- 打开eclipse 时出现Failed to load the JNIshared libraryd的解决方式
由于电脑重装系统,所以电脑的jdk,与eclipse需要重新配置,今天配置完成jdk之后,打开eclipse出现了Failed to load the JNIshared libraryd的错误,如下 ...
- 【置换群】poj3270 Cow Sorting
并不应该叫置换群……只是用到了置换而已,并没有群. 题解看这个吧,我就不写了:http://www.cnblogs.com/kuangbin/archive/2012/09/03/2669013.ht ...
- 【LCA】BZOJ1832 & BZOJ1787(AHOI)-集会
[题目大意] 一个图有n个点n-1条边(也就是说是一棵树),求其中三点共同到达某一点经过总共的最少边数以及共同到达的那一点. [思路] 借用一下黄学长给的结论:三个点两两取LCA,其中必有两个相同,则 ...
- openfire安装完毕后无法登录控制台(忘记密码)的解决方法
openfire登录管理控制提示: Login failed:make sure your username and password are correct and that you’re an a ...
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 10) Large Scale Machine Learning & Application Example
本栏目来源于Andrew NG老师讲解的Machine Learning课程,主要介绍大规模机器学习以及其应用.包括随机梯度下降法.维批量梯度下降法.梯度下降法的收敛.在线学习.map reduce以 ...
- ES6的异步操作
刚开始看书上的这一章的时候,没想到JavaScript还有异步操作,还有这种操作???果不其然,异步操作和java里面的异步操作同样,还是有点难.不过看了两三遍下来,似乎还是明白了一些. 废话不多说, ...
- Spring MVC常用注解@PathVariable、@RequestHeader、@CookieValue、@RequestParam、@RequestBody、@SessionAttributes、@ModelAttribute
简介: handler method参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri部分(这里指uri template中 ...
- nodesj中 中间件express-session的理解
1.为什么使用session? session运行在服务器端,当客户端第一次访问服务器时,可以将客户的登录信息保存. 当客户访问其他页面时,可以判断客户的登录状态,做出提示,相当于登录拦截. sess ...
- 【spring boot】使用定时任务@Scheduled 报错:Encountered invalid @Scheduled method 'dealShelf': Cron expression must consist of 6 fields (found 7 in "0 30 14 * * ? *")
在spring boot中使用使用定时任务@Scheduled 报错: org.springframework.beans.factory.BeanCreationException: Error c ...
- iOS 在某一个ViewController跳转到TabViewController中的某一个ViewController
要做到这个分为两步 第一步, 导入app #import "AppDelegate.h" 第二步, 监听方法中先写加入以下代码: [self dismissViewControll ...