本文为大便一箩筐的原创内容,转载请注明出处,谢谢: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. java.lang.instrument: 一个Java对象占用多少字节?

    一.对象头包括两部分信息:Mark Word(标记字段)和 Klass Pointer(类型指针)   1. Mark Word 用于存储对象自身的运行时数据,如哈希码(HashCode).GC分代年 ...

  2. PyNest——part 4: topologically structured networks

    part 4: topologically structured networks incorporating structure in networks of point neurons 如果我们使 ...

  3. ionic真机调试Android报错 - could not read ok from ADB Server * failed to start daemon * error: cannot connect to daemon

    在使用真机调试Android程序时,报错如下: could not read ok from ADB Server * failed to start daemon error: cannot con ...

  4. HDU2571:命运(简单dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2571 没什么好说的,不过要处理好边界. 代码如下: #include <iostream> # ...

  5. 重置Linux普通账号和root账号密码

    今天想在Linux测试下HTTPie, 突然发现虚拟机里面的Linux, root账号和普通账号密码都忘记了. 百度了半天发现答案都不对, 最后用Google搜到了答案. 本人系统环境: VMware ...

  6. LeetCode:逆波兰表达式求值【150】

    LeetCode:逆波兰表达式求值[150] 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除 ...

  7. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  8. CodeForces - 451E Devu and Flowers (容斥+卢卡斯)

    题意:有N个盒子,每个盒子里有fi 朵花,求从这N个盒子中取s朵花的方案数.两种方法不同当且仅当两种方案里至少有一个盒子取出的花的数目不同. 分析:对 有k个盒子取出的数目超过了其中的花朵数,那么此时 ...

  9. Oracle数据安全(四)j角色管理

    一.角色管理的概述 1.角色的概念 为了简化数据库权限的管理,在Oracle数据库中引入了角色的概念.所谓的角色就是一系列相关权限的集合. 2.角色的特点 在数据库中,角色的名称必须是唯一的,不能与用 ...

  10. datagrid 用法

    http://blog.csdn.net/xhhuang1979/article/details/8331682