【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值
本文为大便一箩筐的原创内容,转载请注明出处,谢谢: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 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 找到旋转后有序数组中的最小值的更多相关文章
- [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 就是找到第一个违反升序的值,就 ...
随机推荐
- docker 构建镜像 centos7 nginx php
#docker 构建镜像(Dockerfile) centos 7.4.1078镜像制作 nginx镜像制作(以前面centos7镜像为基础) Nginx+php镜像制作 更多操作实例,查看git里的 ...
- Android ViewGroup onInterceptTouchEvent
public boolean onInterceptTouchEvent (MotionEvent ev) Implement this method to intercept all touch s ...
- android studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:
今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...
- Python3.6写socket程序
Python进行Socket程序编写使用的主要模块就是 socket 模块,在这个模块中可以找到 socket()函数,该函数用于创建套接字对象.套接字也有自己的方法集,这些方法可以实现基于套接字的网 ...
- restful API(转自阮一峰)
RESTful API 设计指南 网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不 ...
- dobbo 简单框架
- Java基础_基本语法
Java基本语法 一:关键字 在Java中有特殊含义的单词(50). 二:标志符 类名,函数名,变量名的名字的统称. 命名规则: 可以是字母,数字,下划线,$. 不能以数字开头. 见名之意. 驼峰规则 ...
- 使用Linq to XML 修改app.config
使用其他的方法修改app.config无效.而且修改的是*.vshost.exe.Config,程序运行时正常,关闭之后就还是原来的值. Configuration configuration = C ...
- 通过自动回复机器人学Mybatis:代码重构(分层)
imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 ListServlet.java package com.imooc.servlet; ...
- Bean的id、name、ref、refid
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...