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 ...
随机推荐
- sqlserver 三种恢复模式
sql server数据库提供了三种恢复模式:完整,简单和大容量日志,这些模式决定了sql server如何使用事务日志,如何选择它要记录的操作,以及是否截断日志.截断事务日志是删除己执行事务并把该日 ...
- UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现
UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 2) 在系统中, ...
- 如何自学 Python(干货合集)
http://wenku.baidu.com/view/5108f974192e45361066f583.html
- Poj 2777 Count Color(线段树基础)
又毁三观了.......虽然题目数据有坑:区间[a,b]可能会有a>b的情况,但是我一开始没有考虑它也能过. 此外莫名其妙的TLE #include <iostream> #incl ...
- DeDeCMS中如何实现下拉菜单
在5.7版本,已经有比较简单的方法实现下拉菜单,我们可以用它已有方法,也可以用我写的第二种方法来实现 1. 在需要下拉菜单的地方加入以下代码 <div id="navMenu" ...
- [转]Permission denied: /.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable
原文链接:http://blog.csdn.net/dyw/article/details/6612497 近日,在Apache2环境下部署Rails3应用时碰到此错误: Permission den ...
- CCIE路由实验(7) -- MPLS VPN
1.LDP协议的各种情况2.LDP和BGP交互3.LDP高级部分4.MPLS VPN (RIP和静态)5.MPLS VPN (EIGRP)6.MPLS VPN (OSPF)7.MPLS VPN (EB ...
- HDU4544 湫湫系列故事――消灭兔子
HDU 4544 Tags: 数据结构,贪心 Analysis: 将兔子的血量从大到小排序,将箭的杀伤力从大到小排序,对于每一个兔子血量, 将比他大的杀伤力大的剑压入优先队列,优先队列自己重写,让它每 ...
- 向架构师进军--->系统架构设计基础知识
假设你对项目管理.系统架构有兴趣,请加微信订阅号“softjg”,增加这个PM.架构师的大家庭 在解说系统架构设计之前,有必要补充一下架构相关的概念,因此本博文主要讲述架构.架构师和架构设计等相关的概 ...
- 《大象UML》看书笔记2:
<大象UML>看书笔记2 抽象角度: 在为现实世界建模的时候,首先要搞清楚有多 ...