http://changhaz.wordpress.com/2014/10/15/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 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

Solution:
Classic binary search problem. One place worth noticing is the termination conditions. Whenever our window has only one element, its value is the answer. When our window has two elements, and the first element is larger than the second one, the second element is our answer. This case falls into the num[mid]>=num[start] condition in the code below. (given that there is no duplicates in the array)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    int findMin(vector<int> &num) {
        int start=0,end=num.size()-1;
 
        while (start<end) {
            if (num[start]<num[end])
                return num[start];
 
            int mid = (start+end)/2;
 
            if (num[mid]>=num[start]) {
                start = mid+1;
            } else {
                end = mid;
            }
        }
 
        return num[start];
    }
};
 

Leetcode- Find Minimum in Rotated Sorted Array-ZZ的更多相关文章

  1. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

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

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

  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 Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  5. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  6. 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 migh ...

  7. LeetCode——Find Minimum in Rotated Sorted Array II

    Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...

  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——Find Minimum in Rotated Sorted Array

    Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...

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

随机推荐

  1. C# 父窗体与子窗体之间委托

    先实例化子窗体jobForm,然后让 jobForm.TransfEvent += job_TransfEvent;显示子窗体 if (jobForm.DialogResult != DialogRe ...

  2. 推荐-Everything搜索工具

    简介: windows操作系统下极其强大的文件搜索工具. 下载: https://www.voidtools.com/downloads/ 推荐理由: 速度之快难以想象,日常工作必备工具之一. 发现的 ...

  3. python 实现dns 解析发送接收报文

    http://www.qingruxu.com/code/python/851.html https://tools.ietf.org/html/rfc1035里面的图不一定正确,可以使用抓包软件来进 ...

  4. 使用Hive Rest API 连接HDInsight

    以下连接是微软最新的关于HDInsight中Hive命令的RestAPI示例地址.. 使用 HDInsight .NET SDK 运行 Hive 查询 请使用接口有异常的同学检查是否使用的是下面地址中 ...

  5. openerp学习笔记 domain 的应用

    1.在Action中定义,domain用于对象默认的搜索条件: 示例: <record id="action_orders" model="ir.actions.a ...

  6. 浅谈Supermap iClient for JavaScript 弹窗类

    地图作为信息的载体和呈现方式,是GIS的重要组成部分,它是一个浏览信息的窗口,在信息日益发达的今天 ,各种地图应用如雨后春笋一般出现在大众眼前,而不是像以往一样太过局限于专业的领域.而弹窗,是作为地图 ...

  7. 2019美国大学生数学建模竞赛B题(思路)

    建模比赛已经过去三天了,但留校的十多天里,自己的收获与感受依然长存于心.下面的大致流程,很多并没有细化,下面很多情况都是在假设下进行的,比如假设飞机能够来回运送药品,运货无人机就只运货,在最大视距下侦 ...

  8. Xshell关闭导致jar服务终止,使Jar在CentOS后台运行

    环境:Xsehll6,CentOS7 在项目文件夹新建一个runjar.sh 在sh中写入(举例说明) nohup java -Dfile.encoding=UTF- -jar fin-mgmt-.j ...

  9. 查找正序排列的List中缺失的日期数据的一个算法

    Code: public List<DateTime> getMissDateData() { DateTime[] keys = { DateTime.Now.AddDays(-5), ...

  10. MVVM - 事件转命令2

    在使用MVVM模式时, 按照模式的规则是尽量不直接使用事件. 所以对于以前一直使用事件模式的同行来说确实有点头疼. 还好微软给我们提供了几种间接使用事件(命令)的方法, 下面我就来看看这几种方法: I ...