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 ...
随机推荐
- BNU 4096 逆序 思维题
https://www.bnuoj.com/v3/problem_show.php?pid=4096 对于一个序列a,我们定义它的逆序数为满足a[i]>a[j]且i<j的有序对<i, ...
- SSIS-Dtsx包文件打开时一直验证
把每个项的DelayValidation设置为true. 也可以直接改文件: 把文件里的 <DTS:Property DTS:Name="DelayValidation"&g ...
- SQL2000,2005,2008安装在一台机子上
工欲善其事,必先利其器.本机的系统是在网上自己下载的,是32位windows7旗舰版.因为学习,需要在一台机子上同时安装SQL Server2000,2005,2008三个版本的数据库.先是在网上查了 ...
- To the world you may be one person, but to one person you may be the world.
To the world you may be one person, but to one person you may be the world.对于世界而言,你是一个人:但对于某人而言,你是他的 ...
- 【简记】HTML + CSS 的一些要点(不定时更新)
1.td占据多行 / 列时,其挤开的 td 不写(但是包裹 td 的 tr 要写) 2. display:td 的元素中的文本默认垂直不居中(table中的td中的文本是垂直居中的) 3.th虽然定义 ...
- R语言笔记2
<13: Simulation> > sample(1:6,4,replace=TRUE) [1] 4 5 2 6 在1-6的整数中随机生成4个整数,且数字可以重复,即每个数字均可重 ...
- 关于如何将html中的表格下载成csv格式的方法
今天在网上看了很多方法,自己还是慢慢探索写出了最终效果 简单代码如下: <!DOCTYPE html> <html> <head> <meta content ...
- Windows Azure 配置Active Directory 主机(1)
现在越来越多企业将自己业务系统迁移云端,方便公司日常运维管理.这篇文章将简单介绍一下,从 Windows Azure 虚拟网络上的虚拟机 (VM) 中的 Corp Active Directory 林 ...
- Python-OpenCV——亮度和对比度
亮度与对比度 亮度调整是将图像像素的强度整体变大/变小,对比度调整指的是图像暗处变得更暗,亮出变得更亮,从而拓宽某个区域内的显示精度. OpenCV中亮度和对比度应用这个公式来计算:g(x) = αf ...
- JS实现单向链表、双向链表、循环链表
https://cloud.tencent.com/developer/article/1114246 链表存储有序的元素的集合,但是和数组不同的是,链表中的元素在内存中的存储并不是连续的.每一个链表 ...