本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html


原题:

  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.

解释:

  假定有一个有序数组事先按某一个未知的轴发生了旋转,

  (比如 0 1 2 4 5 6 7 旋转后变为 4 5 6 7 0 1 2),

  找到旋转后数组中的最小值,

  假定数组中不存在重复的数据。

思路:

  1. 因为原数组是有序的,所以旋转后的数组的第一个元素必定大于最后一个元素,
  2. 若不满足上述条件,说明数组没有旋转或者旋转轴的位置为0,此时可以直接将第一个元素作为答案返回。
  3. 数组从中间被截断后,原数组中的最小值在数组的后半段被丢弃后仍然是数组中最小值。
  4. 以上述三个条件作为基础,我们可以使用二分法找到数组中的最小元素:

① 使用 head 变量标记二分后数组首元素的位置,tail 标记二分数组的尾元素的位置。

② 若 num[head] > num[tail],则继续执行步骤 ③,否则说明数组满足条件1,此时 num[head] 即为所求的最小数。

③ 使用 med 标记数组最中间的元素位置。

④ 若 num[med] > num[head],说明此时数组的左半段是有序的,则旋转点一定在右半段,因此使 head = med,继续执行步骤 ②。

⑤ 若 num[med] < num[head],说明此时数组的左半段是无序的,则旋转点一定在左半段,因此使 tail = med,继续执行步骤 ②。

⑥ 若 num[med] = num[head],说明此时数组中只有两个或一个元素(数组中不存在重复元素),则旋转点一定是 num[head] 和 num[tail] 中的最小值,所以此时返回它们中的最小值即可。

源码:

// Author DaBianYiLuoKuang
// http://www.cnblogs.com/dbylk/ class Solution {
public:
int findMin(vector<int> &num) {
int size = num.size();
if (!size) {
return ;
} int head = ;
int tail = size - ; while (head <= tail) {
if (num[head] > num[tail]) {
int med = head + tail >> ;
if (num[med] > num[head]) {
head = med;
}
else if (num[med] < num[head]) {
tail = med;
}
else {
return num[head] < num[tail] ? num[head] : num[tail];
}
}
else {
return num[head];
}
} return ;
}
};

【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值的更多相关文章

  1. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  2. LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)

    题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description   姊妹篇:http://www. ...

  3. [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 ...

  4. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  5. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  6. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  7. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  8. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  9. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

随机推荐

  1. jquery获取浏览器类型和版本号的方法

    $(document).ready(function(){ varbrow=$.browser; varbInfo=""; if(brow.msie){bInfo="Mi ...

  2. 流畅的python 对象引用 可变性和垃圾回收

    对象引用.可变性和垃圾回收 变量不是盒子 人们经常使用“变量是盒子”这样的比喻,但是这有碍于理解面向对象语言中的引用式变量.Python 变量类似于 Java 中的引用式变量,因此最好把它们理解为附加 ...

  3. mysql什么情况下使用索引

    表的主关键字 自动建立唯一索引 如zl_yhjbqk(用户基本情况)中的hbs_bh(户标识编号) 表的字段唯一约束 ORACLE利用索引来保证数据的完整性 如lc_hj(流程环节)中的lc_bh+h ...

  4. Spring Security教程 ---- 验证码功能的实现

    有三中方法可以实现验证码的功能 第一种是自定义一个filter,放在SpringSecurity过滤器之前,在用户登录的时候会先经过这个filter,然后在这个filter中实现对验证码进行验证的功能 ...

  5. 什么是web接口

    当我们在请求一个页面的时候,会显示服务器返回的资源,其中包含了HTML.CSS和JS,除此之外,服务器还可以返回图片.视频.字体和插件等类型的资源.这些资源全部由HTTP协议传输. 如果把HTTP协议 ...

  6. getElementsByClassName - 兼容详细介绍

    概述 JavaScript中getElementsByClassName()方法IE8及以下不支持.本文通过使用正则表达式实 现1个兼容方案. 本文内容分为3个部分. 浏览器原生getElements ...

  7. c#中类和成员的修饰符介绍

    类访问修饰符: public 访问级别最高,公共访问没有限制. internal 只允许在本程序集内访问,其他程序集或站点引用其所在的程序集无法访问此类. 例如程序集LibraryA写有ClassA, ...

  8. jsp、freemarker、velocity对比

    在java领域.表现层技术主要有三种:jsp.freemarker.velocity. jsp是大家最熟悉的技术长处:1.功能强大,能够写java代码2.支持jsp标签(jsp tag)3.支持表达式 ...

  9. 『NiFi 学习之路』资源 —— 资料汇总

    一.概述 由于 NiFi 是一个比较新的开源项目,国内的相关资料少之又少. 加之,大家都知道,国内的那么些个教程,原创都只是停留在初级使用阶段,没有更多深入的介绍. 再者,其余的文章不是东抄抄就是西抄 ...

  10. JVM(4) 虚拟机性能监控与故障处理工具

    1. Sun JDK 监控和故障处理工具 1)jps:JVM process Status Tool,显示指定系统内所有的HotSpot虚拟机进程.可以列出正在运行的虚拟机进程,并显示虚拟机执行主类( ...