题目

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
} };

GitHub测试程序源码

LeetCode(162) Find Peak Element的更多相关文章

  1. 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 ...

  2. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  3. 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 ...

  4. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  5. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  6. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  7. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 线程池(4)Executors.newScheduledThreadPool-只执行1次

    例子1:延迟3秒后,只执行1次 ScheduledExecutorService es = Executors.newScheduledThreadPool(5); log.info("开始 ...

  2. (转)linux mount (挂载命令)详解

    linux mount (挂载命令)详解 原文:http://tutu.spaces.eepw.com.cn/articles/article/item/70737 挂接命令(mount) 首先,介绍 ...

  3. Storm编程入门API系列之Storm的Topology的stream grouping

    概念,见博客 Storm概念学习系列之stream grouping(流分组) Storm的stream grouping的Shuffle Grouping 它是随机分组,随机派发stream里面的t ...

  4. ASPX1

    表单提交 <!--表单:收集用户的数据.---> <form method="post" action="AddInfo.ashx"> ...

  5. [转]c# 泛类型(泛型) 以及强类型与弱类型的 理解及优化

    [泛型的概念](1)没有泛型的时候,所有的对象都是以object为基础,如果要使用时必须进行强制类型转换,如果对于值类型,则会导致不断拆箱装箱的过程,对系统消耗很大.(2)使用泛型时不需要通过obje ...

  6. 一个很好用的侧滑框架ICSDrawerController实现的 QQ 侧滑及换肤功能

    使用ICSDrawerController 实现侧滑功能 在ICSDrawerController 第三方上做了修改实现,QQ 点击头像打开关抽屉头像渐变的效果 - (void)hiddenHeadV ...

  7. Game Engine Architecture

  8. SharePoint Server 2016 WEB 网站浏览器支持

    SharePoint Server 2016支持多种常用的Web浏览器,如Internet Explorer,Google Chrome,Mozilla Firefox,Apple Safari和Mi ...

  9. UOJ#7 NOI2014 购票 点分治+凸包二分 斜率优化DP

    [NOI2014]购票 链接:http://uoj.ac/problem/7 因为太麻烦了,而且暴露了我很多学习不扎实的问题,所以记录一下具体做法. 主要算法:点分治+凸包优化斜率DP. 因为$q_i ...

  10. BZOJ 3130: [Sdoi2013]费用流 网络流+二分

    3130: [Sdoi2013]费用流 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 1230  Solved: ...