二分搜索查最小数,from mid to分别为区间的第一个,中位数,和最后一个数

if(from<=mid&&mid<=to)//顺序,第一个即为最小值

return from;

if(from>mid)//发现逆序,则最小值在这个区间,2分搜索区间

to = mid;

if(mid>to)//发现逆序,则最小值在这个区间,2分搜索区间

from = mid;

代码如下:

 class Solution {
public: int findMin(vector<int> &num) {
int size = num.size();
if(size == )
return ;
if(size==)
return num[];
int from = ;
int to = size-; for(int i=;i<size;i++)
{
int mid = (from+to)/;
if(num[from]<=num[mid]&&num[mid]<=num[to])
return num[from];
else if(num[from]>num[mid])
{
if(from+ == mid)
return num[mid];
to = mid;
}
else if(num[mid]>num[to])
{
if(mid+==to)
return num[to];
from = mid;
}
} }
};

Find Minimumd in Rotated Sorted Array的更多相关文章

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

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

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

  3. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

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

  4. [LeetCode] Search 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 ...

  5. 【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 ...

  6. LintCode Find Minimum In Rotated Sorted Array

    1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...

  7. LeetCode-Search in Rotated Sorted Array II

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

  8. Leetcode Find Minimum in Rotated Sorted Array I and II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

随机推荐

  1. PageHelper原理

    1.要是用pagehelper,首先maven项目,要引入 <dependency> <groupId>com.github.pagehelper</groupId> ...

  2. bzoj1003物流运输 最短路+DP

    bzoj1003物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输 ...

  3. QInotifyFileSystemWatcherEngine::addPaths: inotify_add_watch failed: 设备上没有空间

    解决办法: 添加最大监控文件数量 临时解决:(重启之后恢复) 命令设置:sudo sysctl fs.inotify.max_user_watches=524288 生效: sudo sysctl - ...

  4. jdk不同版本的垃圾收集器

  5. anchor-free : CornerNet 和 CenterNet 简要笔记

    CornerNethourglass network -> prediction module = corner pooling -> heatmaps + embedding + off ...

  6. Spring注解驱动开发(四)-----aop、声明式事务

    AOP 概念 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式:-----基于动态代理 一个aop示例 1.导入aop模块:Spring AOP:(spring-aspects ...

  7. 洛谷P4244 [SHOI2008]仙人掌图 II

    传送门 首先不考虑带环的仙人掌,如果只是一棵普通的树,可以通过dp求每棵子树中的最长链和次长链求树的直径. 那么如果dfs的时候遇到了环,应该用环上的两点挂着的最长链加上两点间的距离来更新树的直径,并 ...

  8. hbase连接linux开发过程

    最近近公司被安排做hbase开发,太久没做今天记录下过程 import java.io.IOException; import org.apache.hadoop.conf.Configuration ...

  9. springmvc:配置解决中文乱码的过滤器

    在web.xml中配置以下内容: <!--配置解决中文乱码过滤器--> <filter> <filter-name>characterEncodingFilter& ...

  10. TZOJ 4651 zry和他的的灯泡们(lca)

    描述 zry有一个收集灯泡的习惯,他把灯泡的阴极都共地,阳极连成一颗树,这样的话,他只要在任意一个灯泡的阳极加上合适的电压,所有灯泡都能亮起来.不幸的是,有一对灯泡之间的阳极连线断掉了,这样的话,这颗 ...