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 ...
随机推荐
- PartTime_一些网站
1. http://www.sxsoft.com/ 貌似 搜搜"破解",无符合条件的结果 http://www.taskcity.com/ "智城",貌似 符合 ...
- AI入门丨开源学习资源推荐
现在AI大热,网上的资源也非常多,让人眼花缭乱.非科班的我,经过半年的摸索,也算马马虎虎入了坑.下面整理了我认为不错的学习资源,大部分我都看过,以分享给更多的人.我会不断保持更新,也欢迎大家补充. P ...
- 《springcloud 二》SrpingCloud Zuul 微服务网关搭建
网关作用 网关的作用,可以实现负载均衡.路由转发.日志.权限控制.监控等. 网关与过滤器区别 网关是拦截所有服务器请求进行控制 过滤器拦截某单个服务器请求进行控制 Nginx与Zuul的区别 Ngin ...
- 在 cell 中获取 textFlied内容的使用
当您读到这里时,建议先下载demo,不懂再参考博客.在iOS项目开发中,容易遇到各种个人信息填写.比如微信中设置个人信息,等.这种方式是进行控制器跳转,代理或者block传值,这种比较容易,符合常规的 ...
- Ubuntu批量修改权限
Ubuntu中有两个修改命令可以用到,「change mode」&「change owner」 即chmod以及chown,其中可以用递归参数-R来实现更改所有子文件和子目录的权限. 1.利用 ...
- SharpSvn操作 -- 获取Commit节点列表
/// <summary> /// 获取工作目录的所有节点,包括子目录 /// </summary> /// <param name="workingCopyD ...
- 用指针的方式实现,重写strrchr函数的功能
char *strchrTest(char * ptr,char c); Action(){ char str[]={"thisisadog"}; char c='s'; lr_o ...
- 2013年省市区/县数据SQL Server(SQL语句)
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[tbl_Region]( [ ...
- 虚拟机VMware Workstation Pro装Mac遇到的一些问题【总结】
1. 问题:VM找不到Apple Mac X(M)? 解决方法:在网上找unlocker20*下载: 电脑装一个python3版本以下的版本(装python,主要是编译.因为下载的插件是python写 ...
- .net后台使用post方式对指定地址的方法传值并且获取结果的方法
/// <summary> /// .net 后台 post http地址请求 /// </summary> /// <param name="uri" ...