LeetCode(162) Find Peak Element
题目
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
click to show spoilers.
Note:
Your solution should be in logarithmic complexity.
分析
求一个给定整数序列的峰点。题目假设边界点num[-1] = num[n] = -∞。
方法一:
采用顺序遍历,很容易解决。时间复杂度O(n),需要比较2n次;
方法二:
依然采用顺序遍历,但是深度分析一下题目,时间复杂度O(n),比较次数n;
按照题意,num[0]是大于左边的不存在的那个元素的,num[size−1]也是大于右边那个不存在的元素的, 假如不存在,那么就会有num[0]<num[1],num[1]<num[2],就是增序,num[size−2]<num[size−1], 这样num[size−1]就是peak elem了,所以一定存在。
于是就是这样的思路,num[−1]<num[0],我们假设左边的元素小于右边的元素, 那么第一个左边元素大于右边的那个一定是peak elem。如num[0]。为什么第一个就是呢? 因为前面的都是左<右,判断左>右为false。
方法三:
基于第二个思路,采用二分解决;时间复杂度O(longn)
后两个方法参考文章:网址
AC代码
class Solution {
public:
//方法一:顺序遍历 T(n) = O(n) 比较次数O(2n)
int findPeakElement1(vector<int>& nums) {
if (nums.empty())
return -1;
int len = nums.size();
if (len == 1)
return 0;
for (int i = 0; i < len; ++i)
{
if (i == 0)
{
if (nums[i] > nums[i + 1])
return i;
else
continue;
}//if
if (i == len - 1)
{
if (nums[i] > nums[i - 1])
return i;
else
continue;
}//if
if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1])
return i;
}//for
return 0;
}
/* 按照题意,num[0]是大于左边的不存在的那个元素的,num[size-1]也是大于右边那个不存在的元素的,
* 假如不存在,那么就会有num[0]<num[1],num[1]<num[2],就是增序,num[size-2]<num[size-1],
* 这样num[size-1]就是peak elem了,所以一定存在。
* 于是就是这样的思路,num[NULL] < num[0],我们假设左边的元素小于右边的元素,
* 那么第一个左边元素大于右边的那个一定是peak elem.如num[0].为什么第一个就是呢?
* 因为前面的都是左<右,判断左>右为false。
*/
//方法二:T(n) = O(n) 比较次数O(n)
int findPeakElement2(const vector<int> &num) {//smart O(n), compare n times.
for (int i = 1; i < num.size(); i++){
if (num[i] < num[i - 1])
return i - 1;
}
return num.size() - 1;
}
//方法三:时间O(logn)
int findPeakElement(const vector<int> &num) {
int left = 0, right = num.size() - 1;
while (left <= right){
if (left == right)
return left;
int mid = (left + right) / 2;
if (num[mid] < num[mid + 1])
left = mid + 1;
else
right = mid;
}//while
}
};
LeetCode(162) Find Peak Element的更多相关文章
- LeetCode(215) Kth Largest Element in an Array
题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- [转]Todd.log - a place to keep my thoughts on programming 分布式架构中的幂等性
Todd.log - a place to keep my thoughts on programming 理解HTTP幂等性 基于HTTP协议的Web API是时下最为流行的一种分布式服务提供方式. ...
- JAVA基础之网络通信协议--TCP与UDP
个人理解: 了解区分UDP与TCP的不同,正常情况都是两者结合的使用模式!为了更好的传输,经常会开多线程进行传输的! 一.网络通信协议: 1.TCP/IP协议: 四层:应用层.传输层.网络层和链路层: ...
- Java基础:(七)反射
一.什么是反射 理解反射之前,先要搞懂一件事情,类加载到底是怎么一回事? 类加载相当于Class对象的加载.每个类都有一个Class对象,包含了与类有关的信息.当编译一个新类时,会产生一个同名的.cl ...
- SQL概念及DDL语句
SQL概念 SQL全称(Structured Query Language):结构化查询语句,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询和管理关系型数据库. 其实就 ...
- Elasticsearch-基本操作2
Elasticsearch版本:6.0 为了避免并发修改的冲突问题,数据库中,经常用悲观锁和乐观锁来控制并发问题,而Elasticsearch使用乐观锁.如果源数据在读写过程中被修改,更新将失败,应用 ...
- HDU 3652 B-number (数位DP,入门)
题意: 如果一个整数能被13整除,且其含有子串13的,称为"B数",问[1,n]中有多少个B数? 思路: 这题不要用那个DFS的模板估计很快秒了. 状态设计为dp[位数][前缀][ ...
- js使用my97插件显示当前时间,且select控制计算时间差
做页面需要两个时间输入框一个显示当前时间,一个显示之前的时间,并且需要一个select下拉框控制两个时间输入框之间的差,效果如下图: 这里使用的是My97DatePicer,简单方便,引入my97插件 ...
- java面试题(杨晓峰)---第一讲谈谈你对java平台的理解
本人总结: 面向对象(封装,继承,多态) 平台无关性(jvm运行,class文件) 语言(泛型,lambda) 类库(集合,并发,网络,io/nio) jre(java运行环境,JVM,类库) JDK ...
- [论文理解]Region-Based Convolutional Networks for Accurate Object Detection and Segmentation
Region-Based Convolutional Networks for Accurate Object Detection and Segmentation 概括 这是一篇2016年的目标检测 ...
- 2018.5.13 oracle遇到的问题
安装Oracle 11g 出现交换空间不够 在计算机那里右键打开属性进入高级系统设置然后找到第一个设置找到高级然后更改一下自定义范围(云服务器是16-10000) 然后确定 完成了. 快安装结束之后显 ...