[OJ] Find Minimum in Rotated Sorted Array
LintCode 159. Find Minimum in Rotated Sorted Array (Medium)
LeetCode 153. Find Minimum in Rotated Sorted Array (Medium)
这题看着简单, 但是条件总容易搞错. @_@...
思路是在当前区间有序的时候立即停止, 然后某个点(详见代码)就是答案.
我没有做nums.size() == 0的判断, 因为这种情况应该抛个异常什么的, 给什么int值都可能是有效值(如果nums中的数没有指定范围).
解法1
这个解法要注意一种情况, 就是如果进行R = M - 1之后数组有序了, 最小点应该在R + 1处.
class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
int L = 0, R = n - 1;
while (L <= R) {
int M = (R - L) / 2 + L;
if (num[M] > num[R]) {
L = M + 1;
} else if (num[M] < num[L]) {
R = M - 1;
} else break;
}
return R + 1 < n && num[R + 1] < num[L] ? num[R + 1] : num[L];
}
};
解法1.1
根据上面的分析, 发现那么只进行R = M就可以避免上面的情况了, 于是又写了个更精简的版本.
九章的解法中, target可以换做num[R], 循环条件换成while(L < R), 返回值直接写成num[L], 其实就是这个解法的无break版本, 循环次数会比这个解法多几次, 因为它要一直找到L == R才会结束.
class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
int L = 0, R = n - 1;
while (L < R) {
int M = (R - L) / 2 + L;
if (num[M] > num[R]) {
L = M + 1;
} else if (num[M] < num[L]) {
R = M;
} else break;
}
return num[L];
}
};
解法1.2
受这篇博文启发, 判断"是否有序"可以放到while的判断条件中.
class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
int L = 0, R = n - 1;
while (L < R && num[L] > num[R]) {
int M = (R - L) / 2 + L;
if (num[M] > num[R]) {
L = M + 1;
} else {
R = M;
}
}
return num[L];
}
};
时间复杂度: O(logn)
空间复杂度: O(1)
[OJ] Find Minimum in Rotated Sorted Array的更多相关文章
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- LeetCode OJ 154. 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 II 】python 实现
题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...
- [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 ...
- 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
原题链接在这里: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 就是找到第一个违反升序的值,就 ...
随机推荐
- java反射技术
Class c2 = Class.forName("com.reflection.Test"); // 对类的寻找,找到一个类,注意不是对象 WifiManager mWifiMa ...
- 趣拍proguard配置
# Add project specific ProGuard rules here.# By default, the flags in this file are appended to flag ...
- SqlServer优化博客网址
CareySon Sql Server MVP : http://www.cnblogs.com/CareySon/
- static的用途
1)限制变量的作用域:即在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变: 2)限制变量的存储域:<a>在模块内(但在函数体外),一个被声明为静态的变量,可以被模块内的所 ...
- Win7上安装oracle中可能遇到的错误
安装oracle,总是出现一个警告两个错误错误,其描述是:OUI-18001:不支持操作系统’Windows Vista版本6.1’,找了好久,终于找到原因,因为oracle不支持Win7操作系统. ...
- Objective-C description的用法
description类似于.net/java ToString()方法的用途. 假设有一个CTPerson类, - (NSString *)description { return @"d ...
- iOS-开发日志-UIimageView
UIImageView属性 1.Image 设置图片,默认显示 UIImageView *_imageView = [[UIImageView alloc]init]; _imageView. ...
- 漫话JavaScript与异步·第一话——异步:何处惹尘埃
自JavaScript诞生之日起,频繁与异步打交道便是这门语言的使命,并为此衍生出了许多设计和理念.因此,深入理解异步的概念对于前端工程师来说极为重要. 什么是异步? 程序是分"块" ...
- java I/O技术
一.流的分类 Java的流类大部分都是由InputStream.OutputStream.Reader和Writer这四个抽象类派生出来的 (1)按数据流向 输入流(InputStream类和Read ...
- CCPC网络赛,HDU_5842 Lweb and String
Problem Description Lweb has a string $S$. Oneday, he decided to transform this string to a new sequ ...