【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数
Add Date 2014-10-15
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 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
这个很简单,因为没有重复数字,数组本质上还是有序的,用类似二分查找的方法复杂度O(log n)。记得考虑一下整个数组没有 rotated 的情况。
20号又加了有重复数字的题II,直接附II的 code 吧,II中需要考虑前中后三个数相等的情况,此时无法确定最小值在哪边,只能遍历一遍。
class Solution {
public:
int findMinN(vector<int> &num, int min, int max) {
int minN = num[min];
for(int i = min+; i <= max; ++i) {
if(num[i] < minN)
minN = num[i];
}
return minN;
}
int findMin(vector<int> &num, int min, int max) {
if(min == max || num[min] < num[max])
return num[min];
int mid = (max+min)>>;
if(num[mid] < num[min]) {
return findMin(num, min, mid);
}
else if(num[mid] > num[max]) {
return findMin(num, mid+, max);
}
else
return findMinN(num, min, max);
}
int findMin(vector<int> &num) {
int len = num.size();
return findMin(num, , len-);
}
};
【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数的更多相关文章
- Leetcode33--->Search in Rotated Sorted Array(在旋转数组中找出给定的target值的位置)
题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might becom ...
- [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 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 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 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] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 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 ...
随机推荐
- 可以添加自定义的Select控件
1.控件dom <select name="WebSiteTarget" id="WebSiteTarget" class="w1" ...
- 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计
上一篇讲了Executor接口的设计,目的是将任务的运行和任务的提交解耦.能够隐藏任务的运行策略.这篇说说ExecutorService接口.它扩展了Executor接口,对Executor的生命周期 ...
- 使用HTML5制作简单的RPG游戏
很久以前就想着做一个游戏,但什么都不会又不知道从哪里开始,胡乱找来一些书籍和资料结果太深奥看不懂,无奈只能放弃.这一弃就是十多年,倥偬半生,眼看垂垂老矣,还是没能有什么成果. 近年来游戏引擎越来越多, ...
- Pandoc PDF 中文
最近终于又决定(^_^)使用reStructuredText写文档了,输出PDF时的中文问题必须要解决下. 安装环境 sudo apt install texlive texlive-latex-ex ...
- The Google File System论文拜读
The Google File System Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung Google∗ 摘要 我们设计并实现了谷歌文件系统 ...
- SERVICE_STATUS结构各成员解析
在编写Windows服务的时候,需要调用API函数::SetServiceStatus()向服务控制管理器(SCM)提交更新当前服务的状态信息,其第2个参数为指向SERVICE_STATUS结构的指针 ...
- 资源:Localization – 本地化
Resource Dictionary –资源字典 所有的资源项在最终都会被整合到Resource Dictionary中的,也就是说无论是FrameworkElement的Resources,还是W ...
- CString 成员函数用法大全(转)
CString( );例:CString csStr; CString( const CString& stringSrc );例:CString csStr("ABCDEF中文12 ...
- python中装饰器你真的理解吗?
def w1(func): print('装饰器1....') def w1_in(): print('w1_in.....') func() return w1_in def w2(func): p ...
- 响应式布局【3】 --- bootstrap
本片文章主要讲解Bootstrap中如何实现响应式布局的. 参考资料&内容来源: https://code.ziqiangxuetang.com/bootstrap/bootstrap-gri ...