Leetcode:find_minimum_in_rotated_sorted_array
一、 题目
给定一个排好序的数组。数组可能是单调递增,也可能有一个变换。
(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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- A - Oil Deposits(搜索)
搜索都不熟练,所以把以前写的一道搜索复习下,然后下一步整理搜索和图论和不互质的中国剩余定理的题 Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp ...
- Word中使用代码高亮插件
Word中使用代码高亮插件 1.下载并安装:SyntaxHighlighter4Word.zip 解压,然后双击bin\word2010\Kong.SyntaxHighlighter.Word2010 ...
- zIndex属性在IE中无效
在ie中他的子类的zindex就以父类为准: <!doctype html> <html> <head> <meta charset="utf-8& ...
- 《算法导论》读书笔记之图论算法—Dijkstra 算法求最短路径
自从打ACM以来也算是用Dijkstra算法来求最短路径了好久,现在就写一篇博客来介绍一下这个算法吧 :) Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的 ...
- svn修改log信息
在linux下安装了SVN服务器来做版本控制. 有天提交文件忘记了填写SVN提交日志,于是在项目中使用右键,show log,找到我提交的无日志的那条记录,点击右健,选择了“Edit log mess ...
- 17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制对于新的Master和Slaves:
17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制对于新的Master和Slaves: 最简单和最直接的方法是设置复制用于使 ...
- AsyncTask使用须知
AsyncTask的实现原理就是封装了的线程池,详细见AsyncTask实现原理. 在1.5中初始引入的时候, AsyncTask 运行( AsyncTask.execute() )起来是顺序的,当同 ...
- Objective-C KVC 自己主动转换类型研究
## Objective-C KVC 自己主动转换类型研究 apple非常厚道,kvc的时候帮我们做了一些类型转换,规律贴出来,给大伙參考參考 @interface Entity : NSObject ...
- Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)
目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...
- java--进步学习IO
import java.io.*; public class Demo1 { public static void main(String []args) throws Exception{ File ...