[Leetcode] Find the minimum in rotated sorted array
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步。
题目:
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.
Tag:
Array; Binary Search
体会:
常规binary search, 但不同于先前去找insert position的那道题。先前那道题要找的target可能是不出现在array中,但是这道题target一定出现在array中,所以循环条件相应改变了,变成当low和high指向同一个index时就停止了。
1. 每次在去找中点值之前,如果当前A[low] - A[high]已经是sorted的部分了,就可以直接返回A[low]了。
2. 在跳动指针的时候注意,如果是A[mid] > A[high], 指针low可以直接跳过mid,i.e. low = mid + 1, 这是因为既然A[mid]比A[high]大了,那mid上的一定不会是最小值了。相对应的,如果是A[mid] <= A[high]的情况,就不能跳过mid, 因为从这个不等式来看,mid是可能成为最小值的,所以只能有 high = mid.
class Solution {
public:
int findMin(vector<int> &num) {
int low = ;
int high = num.size() - ;
// binary search ends when low and high
// points to the same index
while (low < high) {
// already found the sorted part?
if (num[low] < num[high]) {
return num[low];
}
int mid = low + (high - low) / ;
if (num[mid] > num[high]) {
// since num[mid] > num[high],
// num[mid] cannot the minimum, we can
// jump to mid + 1 safely.
low = mid + ;
} else {
// num[mid] <= num[high],
//so mid might be the position we want
// we can not skip mid
high = mid;
}
}
return num[low];
}
};
[Leetcode] Find the minimum in rotated sorted array的更多相关文章
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 【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 JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...
- [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 154. Find Minimum in Rotated Sorted Array II --------- java
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [LeetCode#154]Find Minimum in Rotated Sorted Array II
The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II_Hard
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 II 】python 实现
题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...
随机推荐
- 使用canvas来实时播放RTSP视频
HTML5的标签可以用使用下来面的方式来播放静态视频 <video width="320" height="240" controls="con ...
- 视差滚动(Parallax Scrolling)效果的原理与实现
视差滚动(Parallax Scrolling)效果的原理与实现1.视差滚动效果的主要特点: 1)直观的设计,快速的响应速度,更合适运用于单页面 2)差异滚动 分层视差 页面上很多的 ...
- [转]JS基础之undefined与null的区别
在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...
- Django数据迁移
http://www.ziqiangxuetang.com/django/django-data-migration.html
- Java之集合类
出处:http://blog.csdn.net/zhangerqing 一.集合类简介 数组是很常用的一种的数据结构,我们用它可以满足很多的功能,但是,有时我们会遇到如下这样的问题: 1.我们需要该容 ...
- DataTables给每一列添加下拉框搜索
$(document).ready(function() { $('#example').DataTable( { initComplete: function () { var api = this ...
- unity3D对象的显示和隐藏
SetActive/active/SetActiveRecursively 后两者比较旧,现在通常用第一个SetActive 必须先new一个gameobject对象用于实例化,然后再设置其activ ...
- android run process
http://www.jb51.net/article/32798.htm http://www.elecfans.com/tongxin/119/20120315263977.html 图 1 详细 ...
- man手册导出成txt,pdf,html的一些小技巧
经常man一些shell命令,有时候有想导出来编辑或注释一下,所以要导出.方法有很多种,根据自己的实际需要觉得比较实用的记录下分享一下. 1.导出成txt man –t bash |col –b &g ...
- BZOJ 2521 最小生成树(最小割)
http://www.lydsy.com/JudgeOnline/problem.php?id=2521 题意:每次能增加一条边的权值1,求最小代价让一条边保证在最小生成树里 思路:如果两个点中有环, ...