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 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
二分查找就行了。
class Solution {
public:
int findMin(vector<int> &arr) {
if (arr.empty()) return -;
int l = , h = arr.size() - , m, min = INT_MAX;
while (l <= h) {
m = l + (h - l) / ;
if (arr[m] >= arr[l]) {
if (min > arr[l]) min = arr[l];
l = m + ;
} else {
if (arr[m] < min) min = arr[m];
h = m - ;
}
}
return min;
}
};
Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
The array may contain duplicates.
class Solution {
public:
int findMin(vector<int> &arr) {
if (arr.empty()) return -;
int l = , h = arr.size() - , m, min = INT_MAX;
while (l <= h) {
m = l + (h - l) / ;
if (arr[m] > arr[l]) {
if (min > arr[l]) min = arr[l];
l = m + ;
} else if (arr[m] < arr[l]) {
if (arr[m] < min) min = arr[m];
h = m - ;
} else {
if (arr[m] < min) min = arr[m];
l++;
}
}
return min;
}
};
Leetcode | Find Minimum in Rotated Sorted Array I && II的更多相关文章
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [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 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
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- Find Minimum in Rotated Sorted Array I & II
Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...
- Find Minimum in Rotated Sorted Array I&&II——二分查找的变形
Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...
随机推荐
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- iOS UIImageView设置为圆形
UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...
- Redis入门指南
随着互联网业务对性能需求日益强烈,作为Key/Value存储的Redis具有数据类型丰富和性能表现优异的特点.如果能够熟练地驾驭它,不管是把它用做缓存还是存储,对很多大型应用都很多帮助.新浪作为世界上 ...
- oracle的内置函数
1.wmsys.wm_concat 行转列函数 select wmsys.wm_concat(destnumber) from mms_send_his_record group by sendn ...
- 每天一个linux命令---telnet
执行telnet指令开启终端机阶段作业,并登入远端主机. telnet的命令的格式: telnet ip port 例1: 建立连接不成功 [richmail@portal bin]$ telne ...
- 【SAP BO】BOE 4.1版本新特性
为了更好地向用户推广BusinessObjects BI 4.1版本的新特性,SAP公司宣布将在2012年中旬推出针对BusinessObjects BI 4.0的功能补丁程序(Feature Pac ...
- 【原】iOS学习45之多媒体操作
1. 音频 1> 音频实现简述 iOS 里面共有四种专门实现播放音频的方式: System Sound Services(系统声音服务) OpenAL(跨平台的开源的音频处理接口) Audio ...
- BZOJ2990 : [Ontak2010]Keyboard
考虑从$(1,1)$开始搜索移动方案,每次移动坐标的变化量都是$2$. 如果构成了环,那么环的周长肯定是偶数. 考虑这个环一定要被若干个骨牌覆盖,且还有一个位置是空的. 所以得出环的周长是奇数,矛盾, ...
- session过期问题
php中session过期时间设置及回收机制详解: 修改php中的session过期时间可以修改php配置文件php.ini中的session.gc_maxlifetime即可. 当php每发出一次请 ...
- Android Fragment 生命周期及其API使用(建议使用自定义View替换Fragment)
我为什么不主张使用Fragment Fragment:( Fragment就相当于一个有生命周期的View,它的生命周期被所在的Activity的生命周期管理 ) 生命周期回调说明: onAttach ...