[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 就是找到第一个违反升序的值,就 ...
随机推荐
- Servlet & JSP - Decorating Requests and Responses
Servlet API 提供了四个包装类:ServletRequestWrapper.ServletResponseWrapper.HttpServletRequestWrapper.HttpServ ...
- jdk在windows中的配置
1.下载jdk(java developer kit),其内部包含jre(java runtime environment): 安装解压缩到一盘内,如:G:\Program Files\Java: 2 ...
- cordova安装中的坑
1.安装android环境直接略过! 2.安装node.js直接略过! 3.安装cordova npm install -g cordova npm uninstall cordova -g(这条是 ...
- Mac OS 安装 Port
简介 MacPorts类似与apt-get以及yum等软件包管理工具,可以方便的进行安装与卸载软件的功能,同时可以自动安装软件包的依赖,非常方便,同类的还有brew等工具. 安装 下载MacPorts ...
- important的妙用
!important: 为某些样式设置具有最高权值,高于id选择器 用法: !important要写在分号的前面 例如: <p class="first">!impor ...
- php练习6——面向对象编程(打印乘法表)
要求:编写一个成员函数,从键盘输入一个数(0—9),打印出对应的乘法表 程序:viewChengFB.html chengFB.class.php printChengFB.php 结果
- CentOS使用sudo提示用户不在sudoers文件中的解决方法
1切换到root用户[linux@localhost ~]$ su root密码:[root@localhost ~]# 2查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 [roo ...
- 读取Android APK文件签名的方法
在微信开放平台等申请API key 和secret时经常要用到apk文件签名,那么如何读取呢? 下面贴一下相关读取源码: 一共两个文件MainActivity和MD5, package com.lcg ...
- 使用South时候由于两个相同id的文件引起的问题
由于之前版本控制的一个小失误, 在主分子上面调用python manage.py makemigrations生成了 0058_auto__add_unique_setting_name_value. ...
- C#中的委托用法
当一个函数有返回值的时候用,用Func委托方法. 例如: static int sum(int x) { return x+x; } Func<int> a = sum; 当一个函数没有返 ...