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 ...
随机推荐
- java类和对象的基础(笔记)
在Java类的设计中,有时希望一个类在任何时候只能有一个实例.这时可以将该类设计为单例模式(singleton).要将一个类设计为单例模式,需要把类的构造方法的访问修饰符声明为private,然后在类 ...
- BZOJ 1911: [Apio2010]特别行动队( dp + 斜率优化 )
sum为战斗力的前缀和 dp(x) = max( dp(p)+A*(sumx-sump)2+B*(sumx-sump)+C )(0≤p<x) 然后斜率优化...懒得写下去了... ------- ...
- 树莓派做web服务器(nginx、Apache)
一想到Linux Web服务器,我们首先想到的是: Apache + MySql + Php. Apache:是世界使用排名第一的Web服务器软件. 可以运行在几乎所有广泛使用的计算机平台上,由于其跨 ...
- iOS- 三步快速集成社交化分享工具ShareSDK
http://www.cnblogs.com/qingche/p/3727559.html 1.前言 作为现在App里必不可少的用户分享需要,社交化分享显然是我们开发app里较为常用的. 最近因为公司 ...
- Qt 5.x 全局热键 for windows
Qt 升级到5.x版本后,QAbstractEventDispatcher中函数发生变动,导致libqxt库中的qxtGlobalShortcut挂掉.参考qxtGlobalShortcut写了一个全 ...
- td
http://jameswxx.iteye.com/blog/1041173 http://crane-ding.iteye.com/blog/968862 http://www.ibm.com/de ...
- c++ namespace命名空间详解
What is a namespace? A namespace defines an area of code in which all identifiers are guaranteed to ...
- Unity3D 4.x 使用Mecanim实现动画控制
Unity3D 4.x 版本号之后提供了一种新的动画机制Mecanim,尽管眼下还支持之前的Animation.但看到Unity3D 4.3 预览版里Sprite的动画也是基于Animator的,可知 ...
- BT基础知识简介
1. 蓝牙概述 无线局域网的通信 适用范围:10米到100米(根据发射功率的class不同有所差别,典型的class2为10m,而class1为100m,class3为1m) 应用: 局域网络 ...
- BZOJ 2429: [HAOI2006]聪明的猴子( MST )
水题, 求MST即可. -------------------------------------------------------------------------------- #includ ...