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 ...
随机推荐
- Netty(1-2)Discard Client
一.DiscardClientHandler import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; import ...
- .net程序员业余Android开发赚点外快(介绍一下自己的经验)
记得是11年10月份开始研究android的,当时还不会java,听说android比较火,自己也买了个垃圾android机,平时工作也不是特别忙,于是我就突发奇想,想试试做一下android应用可不 ...
- feign容断忽略某些异常
@HystrixCommand(ignoreExceptions={ BusinessException.class, IllegalArgumentException.class, BadCrede ...
- Kendo MVVM 数据绑定(四) Disabled/Enabled
Kendo MVVM 数据绑定(四) Disabled/Enabled Disabled 和 Enabled 绑定可以根据 ViewModel 的某个属性值的 true,false 来设置 DOM 元 ...
- SDUT 1309 不老的传说问题 (区间DP)
题意: 有一个环形序列,n个数字表示一种颜色,要求将白板环刷成一模一样的环,限制是每次最多只能刷连续的K个位置,问最少需要刷几次? 思路: 跟2008长春那道painter string 差不多.只是 ...
- UWP开发:应用文件存储
应用设置由于数据量和数据类型的限制,有很大的局限性,所以还需要应用文件存储,以文件的方式存储数据.在每个应用的应用数据存储中,该应用拥有系统定义的根目录:一个用于本地文件,一个用于漫游文件,还有一个用 ...
- jquery.restrictFieldLength.js
1.参考资料 http://www.cnblogs.com/aarond/archive/2013/08/02/3234042.html 2.使用举例 //字符控制 $(function () { $ ...
- java面试题(杨晓峰)---第三讲谈谈final、finally、finalize有什么不同?
java语言有很多看起来相似,但用途却完全不相同的语言要素,这些内容往往容易成为面试官考察你知识掌握程度的切入点. 今天我要问你一个基础的java经典题目,谈谈final.finally.finali ...
- UVA 11552 Fewest Flops(区间dp)
一个区间一个区间的考虑,当前区间的决策只和上一次的末尾有关,考虑转移的时候先统计当前区间出现过的字母以及种数ct 枚举上一个区间的末尾标号j,规定小于INF为合法状态,确定j之后看j有没有在当前的区间 ...
- UVA 1451 Average平均值 (数形结合,斜率优化)
摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...