本文为大便一箩筐的原创内容,转载请注明出处,谢谢: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
),
找到旋转后数组中的最小值,
假定数组中不存在重复的数据。
思路:
- 因为原数组是有序的,所以旋转后的数组的第一个元素必定大于最后一个元素,
- 若不满足上述条件,说明数组没有旋转或者旋转轴的位置为0,此时可以直接将第一个元素作为答案返回。
- 数组从中间被截断后,原数组中的最小值在数组的后半段被丢弃后仍然是数组中最小值。
- 以上述三个条件作为基础,我们可以使用二分法找到数组中的最小元素:
① 使用 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 D*YiLuoKuang
// 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 找到旋转后有序数组中的最小值的更多相关文章
-
[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. 思路 ...
-
LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
-
[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 Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
-
LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
-
[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 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
-
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 就是找到第一个违反升序的值,就 ...
随机推荐
-
Windows系统下Oracle每天自动备份
linux和unix下面使用shell可以很方便实现,如果windows环境下可以结合计划任务实现 创建备份目录d:\backup, 创建批处理命令Bak.bat,编写备份脚本 exp user/pa ...
-
VC++获取IDC_EDIT的7种方法
VC++获取IDC_EDIT的7种方法 http://blog.csdn.net/baizengfei/article/details/7997618 //第一种方法 int number1, num ...
-
【翻译】Tomcat 6.0 安装与启动
本篇来自Tomcat6官方文档:运行手册running.txt 有很多以前都没注意的问题,这里正好学习下. 系列文章来自:<Tomcat官方文档翻译> Tomcat的安装 1 确认本机是否 ...
-
PHP 安装 eaccelerator
安装开发工具包: yum groupinstall -y "Development Tools" 查看本机php版本: rpm -qi php 下载rpm包: wget http: ...
-
Style绑定
目的 style绑定可以添加或者移除DOM元素的样式值.这非常有用,例如,当值为负数时将颜色变为红色. (注:如果要修改CSS整个类,请使用css绑定) <div data-bind=" ...
-
mpvue小程序开发之 wx.getUserInfo获取用户信息授权
一.背景 在使用美团的mpvue2.0框架搭建起小程序项目后,做获取用户信息时遇到一些问题:微信小程序更新api后,获取用户信息只能通过button上的绑定方法 来获取用户信息,vue上方法绑定不能直 ...
-
java代码之美(4)---guava之Immutable(不可变)集合
Immutable(不可变)集合 一.概述 guava是google的一个库,弥补了java语言的很多方面的不足,很多在java8中已有实现,暂时不展开.Collections是jdk提供的一个工具类 ...
-
去中心化存储项目终极指南 | Filecoin, Storj 和 PPIO 项目技术对比(下)
在上篇文章中,我们主要从价值定位.技术层次架构.服务质量.去中心化程度,和经济激励机制五个方面分析了三个项目的不同.在这一篇文章中,我们将着重从区块链的架构设计.数据传输技术设计和数据存储技术设计三方 ...
-
【C#】详解C#异常
目录结构: contents structure [+] 异常处理机制 try块 catch块 finally块 自定义异常 CLS异常和非CLS异常 在这篇文章中,笔者会阐述C#中的异常.C#是一门 ...
-
深入浅出Android开发之Surface介绍
一 目的 本节的目的就是为了讲清楚Android中的Surface系统,大家耳熟能详的SurfaceFlinger到底是个什么东西,它的工作流程又是怎样的.当然,鉴于SurfaceFlinger的复杂 ...