二分搜索查最小数,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. node.js 安装步骤

    1.打开链接(http://nodejs.cn/download/) 2.下载自己所需的安装包(32位 or 64位.哪个系统) 3.双击直接安装(成功如下图) 4.点击 Node.js comman ...

  2. 原生JS实现动态时钟(优化)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 转载:JVM内存分代策略

    Java虚拟机根据对象存活的周期不同,把堆内存划分为几块,一般分为新生代.老年代和永久代(对HotSpot虚拟机而言),这就是JVM的内存分代策略. 为什么要分代? 堆内存是虚拟机管理的内存中最大的一 ...

  4. Hibernate-HQL-Criteria-查询优化

    1 查询总结 oid查询-get 对象属性导航查询 HQL Criteria 原生SQL 2 查询-HQL语法 2.1 基础语法 2.2 进阶语法 排序 条件 分页 聚合 投影 多表查询 SQL HQ ...

  5. Blow up the city

    Blow up the city 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Country A and B are at war. Country A needs to organ ...

  6. node学习记录——搭建web服务器

    web服务器的基本知识 功能:1.接收HTTP请求(get,post,delete,put)2.处理HTTP请求 常见的web服务器架构: 1. Nginx/Apache:负责接收http请求,确定谁 ...

  7. Linux安装Desktop 和 vncserver

    sudo su - #使用 root 账户 yum grouplist #查看所有可用的group yum groupinstall GNOME Desktop #安装 GNOME 桌面 yum -y ...

  8. 【linux配置】Linux同步网络时间

    Linux同步网络时间 1.date '+%Y%M%D' 按照格式显示当前日期,结果如下: [root@LAMP ~]# date "+%Y-%m-%d %H:%M:%S" -- ...

  9. Xshell 、PuTTY 复制文件到Linux

    一.使用Xshell 在linux下下载一个需要安装一个工具lrzsz包: [root@localhost home] # yum install -y lrzsz 从windows上传文件到linu ...

  10. Asp.Net Core2.0在linux下发布

    一.在linux上新建mvc项目发布 可以参考:https://segmentfault.com/a/1190000012428781 也可以看微软官方文档. 大致步骤如下: 1.在linux下安装. ...