【leetcode】Find Peak Element
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.
Note:
Your solution should be in logarithmic complexity.
利用二分法才能达到logarithmic的复杂度
当num[mid]<num[mid+1]时,peek一定在右边
当num[mid]>num[mid+1]时,peek一定在左边
class Solution {
public:
int findPeakElement(const vector<int> &num) {
int n=num.size();
int left=;
int right=n-;
int result;
int mid;
while(left<right)
{
mid=(left+right)/;
if(mid+>n-)
{
return mid;
}
if(num[mid]<num[mid+])
{
left=mid+;
}
else if(num[mid]>num[mid+])
{
right=mid;
}
}
return left;
}
};
【leetcode】Find Peak Element的更多相关文章
- 【leetcode】Find Peak Element ☆
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 【Leetcode】【Medium】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 【数组】Find Peak Element
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【LeetCode】961. N-Repeated Element in Size 2N Array 解题报告(Python & C+++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
- 【LeetCode】540. Single Element in a Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:异或 方法二:判断相邻元素是否相等 方法三:二分查找 ...
- 【leetcode】Kth Largest Element in an Array (middle)☆
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 【LeetCode】169 - Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- php函数mt_rand和rand 速度测试
今天在写代码时,看到以前的同时写了一个取随机数,用到了mt_rand(2,19) 就顺手搜了一下,mt_rand和rand的区别. 先看官方的解释 mt_rand 和 rand mt_rand — 生 ...
- MySQL中concat函数(连接字符串)
MySQL中concat函数使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串 ...
- golang笔记——环境搭建
1.下载安装 从 https://golang.org/dl/ 这里下载最新版本的 golang 安装包,分别有 Windows\Linux\Apple OSX\源码包. golang的官方网站是 h ...
- vue-cli + webpack 多页面实例应用
关于vue.js vue.js是一套构建用户界面的 轻型的渐进式前端框架.它的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.使用vue可以给你的开发带来极致的编程体验. 关于vu ...
- 迷你版Deferred
直接贴代码: /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { ret ...
- 条件查询N多的情况下,回显解决方法。
条件查询每个web程序员一定都写过,关于条件回显值页面的思路很简单,将页面的值传到后台,放置request作用域,然后回显至页面. 如果几个条件还好些,如果是下面这种情况呢? 如果条件像以上情况N多情 ...
- MongoDB MapReduce学习笔记
http://cnodejs.org/topic/51a8a9ed555d34c67831fb8b http://garyli.iteye.com/blog/2079158 MapReduce应该算是 ...
- linux下的库冲突问题
lib1.c #include <stdio.h>int fun(){ printf("lib1\n"); return 0;} lib2.c #include < ...
- WP7/WP8/WP8.1设置存储的区别
最近做的一个软件,由于最初是针对WP7的,现在在只能创建WP8.1的visual studio 2013上转换为了WP8的. 由于要保存字体大小.背景颜色.JSON三个数据,一直使用的是WP7下的存储 ...
- javaweb 解决将ajax返回的内容用document.write写入,FireFox一直加载的问题
在document.write方法后加上document.close就解决了, 想知道原理的小伙伴可以继续看 浏览器在解析html时会打开一个流,这是用document.write中写入,是加入当解析 ...