一、     题目

给定一个排好序的数组。数组可能是单调递增,也可能有一个变换。

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2)

要求找出最小的数。

二、     分析

这道题正如题目所说的分来两种情况

1、         严格单调递增

2、         有一个拐点

我们须要分情况处理。当然也能够无论这些直接遍历,即从头到尾扫描,找出最小的数;假设依据题意,我们须要考虑

1、         当严格单调时,因为是排好序的所以我们直接return 第一个元素就可以;或者直接利用二分法查找也能够。

2、         当有拐点时,我们就能够二分查找高速找到最小值。

综上所诉。考虑两种情况能够提高效率,可是直接当做一种情况更优点理。

并且都能通过。

class Solution {
public:
int findMin(vector<int> &num) {
int min = num[0];
for(int i=1;i<num.size();i++){
if(num[i]>num[i-1])
min = num[i];
}
return min;
}
}; class Solution {
public:
int findMin(vector<int> &num) {
if(num.size()==0) return 0;
int sta=0;
int flag;
int end=num.size()-1;
while(sta<end){
flag = (sta+end)/2;
if(num[flag]<num[end])
end=flag;
else
sta=flag+1;
}
return num[sta];
}
}; class Solution {
public:
int findMin(vector<int> &num) {
int cou=num.size()-1; //the numbers of num
if(num.size()==0) return 0; //the num is null
if(num.size()==1) return num[0]; //num have a single number
int mid=0;
int sta=0; //the start
int end=cou; //the end
if(num[sta]<num[end]) { //Monotonically increasing
return num[0];
}
else { //There are situations inflection point
while(sta<end){
mid = (sta+end)/2;
if(num[mid]<num[end])
end=mid;
else
sta=mid+1;
}
return num[end];
}
}
};

Leetcode:find_minimum_in_rotated_sorted_array的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. PHPCMS V9.3.2用户注册模板中的一个低级Bug

    当我们下载了目前最新的PHPCMS的时候,如果我们修改了用户注册的模板,那么模板缓存就会更新,这时候就会如下 的问题: Parse error: syntax error, unexpected T_ ...

  2. activity变成Dialog的步骤

    1.在布局文件上最外层最好使用RelativeLayout来布局,如果使用LinearLayout来布局的话,显示对话框的话,感觉会有点问题: 要在预览中看到框框,并且是match_parent的,而 ...

  3. SB中使用Autolayout设置到父视图的间距为0

    发现打上钩的话在plus以下的屏幕上控件的实际x会是-4但是在plus上是0, 所以要设置一致的到视图的左右都未0 必须把Constrain to margins钩去掉!

  4. PHP学习笔记8-文件操作

    在data文件中写入数据: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/29 * Time: 17 ...

  5. JavaScript学习笔记(四十四) 装饰器

    装饰器模式(Decorator) 在装饰器模式中,可以在运行时给一个对象动态的添加额外的功能.当和静态类打交道的时候(static classes),这可能是一个挑战.但在JavaScript中,对象 ...

  6. QTP的基本功能介绍

    • QTP的基本功能介绍 HP QuickTest Professional 支持功能測试和回归測试自己主动化,用于每一个主要软件应用程序和环境.此解决方式使用keyword驱动的測试概念,简化了測试 ...

  7. BZOJ 3438: 小M的作物( 最小割 )

    orz出题人云神... 放上官方题解... 转成最小割然后建图跑最大流就行了... ---------------------------------------------------------- ...

  8. 【转载】CentOS日志系统组成详解

    日志系统有三部分组成:一.使用什么工具记录系统产生的日志信息?      syslog服务脚本管理的两个进程: syslogd.klogd 来记录系统产生的日志信息:      klogd     进 ...

  9. Jquery学习笔记:删除节点的操作

    假设如下的html代码 <div id="mydiv" style="width:100px;height:100px;border:1px solid red&q ...

  10. 如何在VMware中修改Mac OS的屏幕分辨率

    关于mac os分辨率问题:方法一:临时方法,只对当次启动有效,即在启动倒计时的时候,回车,等待输入参数是输入如下文本:“Graphics Mode"="1280x800x32@6 ...