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.
题解:二分。从mid开始找,找到了就返回结果。如果mid不是,哪边比mid大,结果必然在那一边的序列里面。
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n=nums.size();
nums.push_back(numeric_limits<int>::min());
int l=,r=n;
int mid;
while(l<r){
mid=(l+r)/;
if((mid==&&nums[mid]>nums[])||(nums[mid-]<nums[mid]&&nums[mid+]<nums[mid])){
return mid;
}
else if(nums[mid-]>nums[mid]){
r=mid;
}
else{
l=mid+;
}
}
return mid;
}
};
leetcode 162 Find Peak Element(二分法)的更多相关文章
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- LeetCode 162.Find Peak Element(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- ✡ leetcode 162. Find Peak Element --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- Java并发专题 带返回结果的批量任务执行
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/27250059 一般情况下,我们使用Runnable作为基本的任务表示形式,但是R ...
- unity shader 编辑器扩展类 ShaderGUI
这应该unity5才出的新功能了,今天看文档时刚巧看到了,就来尝试了一下. 效果如图: shader 的编辑器扩展分为2种方法: 是通过UnityEditor下的ShaderGUI类来实现的,形式比较 ...
- 微信小程序之云开发一
最近听说微信小程序发布了云开发,可以不需要购买服务器,就能开发小程序和发布小程序,对于动辄千元的服务器,极大的节约了开发成本,受不住诱惑,我就开始了小程序的云开发,目前项目已上线,亲测不收费,闲不住的 ...
- MySQL的max()函数使用时遇到的小问题
通常我们获取某个表的某个字段最大值时可以使用max()函数. 使用场景举例: 获取某个表id的最大值:SQL: SELECT max(id) FROM table_name; SELECT max(` ...
- centOS6.2 最小安装下的无线网络配置
一.安装wireless_tools,http://www.linuxfromscratch.org/blfs/view/svn/basicnet/wireless_tools.html 二.vi / ...
- j2EE的web.xml详解
https://blog.csdn.net/changqing5818/article/details/49928231 https://www.cnblogs.com/ClassNotFoundEx ...
- 浅谈<持续集成、持续交付、持续部署>(二)
集成是指软件个人研发的部分向软件整体部分交付,以便尽早发现个人开发部分的问题:部署是代码尽快向可运行的开发/测试节交付,以便尽早测试:交付是指研发尽快向客户交付,以便尽早发现生产环境中存在的问题.如果 ...
- [Delphi]解决Delphi Distiller运行报错"HKEY_CURRENT_USER\\" is of wrong kind or size
最近终于决心将使用多年的Delphi 7升级到Delphi 2007,虽然目前Delphi最高版本已经是XE8,但对于只做VCL开发的话还是喜欢2007这个经典的版本. 安装Delphi 2007一切 ...
- [Android]豆瓣FM离线数据
离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...
- spider_action
spider from mobile to mobile to mobile from selenium import webdriver from selenium.webdriver.chrome ...