[LeetCode] 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.
Your solution should be in logarithmic complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
#include <vector>
#include <iostream>
using namespace std; class Solution {
public:
int findPeakElement(const vector<int> &num) {
int n = num.size();
if(n==&&num[]<num[]&&num[]>num[]) return ;
if(n<) return ;
if(num[]>num[]) return ;
if(num[n-]<num[n-]) return n-;
return help_fun(num,,num.size()-);
}
int help_fun(const vector<int> &num,int lft,int rgt)
{
int n = num.size();
if(rgt - lft <){
if(lft!=&&num[lft-]<num[lft]&&num[lft]>num[lft+])
return lft;
if(rgt!=n-&&num[rgt-]<num[rgt]&&num[rgt]>num[rgt+])
return rgt;
return -;
}
int mid = (lft+rgt)/;
if(num[mid-]<num[mid]&&num[mid]>num[mid+]) return mid;
int ascend = -;
if(num[mid-]<num[mid]&&num[mid]<num[mid+]) ascend = ;
if(num[mid-]>num[mid]&&num[mid]>num[mid+]) ascend = ;
if(ascend==&&num[mid]>=num[rgt])
return help_fun(num,mid,rgt);
if(ascend==&&num[lft]<-num[mid])
return help_fun(num,lft,mid);
int retlft = help_fun(num,lft,mid);
if(retlft!=-) return retlft;
return help_fun(num,mid,rgt);
}
}; int main()
{
vector<int > num{,,};
Solution sol;
cout<<sol.findPeakElement(num)<<endl;
return ;
}
[LeetCode] Find Peak Element 二分搜索的更多相关文章
- LeetCode Find Peak Element [TBD]
说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- Lintcode: Find Peak Element
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- LeetCode OJ 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 ...
随机推荐
- DeepFaceLab报错, Could not create cudnn handle 解决方法!
DeepFaceLab 虽然没有可视化界面,但是在众多换脸软件中,是安装最方便,更新最快,整体性能最佳的一个.这个软件对于系统依赖很低,也就是不需要装各种各样的“插件”. 但是即便如此,由于版本的不断 ...
- 关于request.getServletContext()方法报错的问题
可以通过修改pom文件来添加一个javax.servlet-api-3.1.0.jar的jar包,找到你的pom.xml文件添加代码如下: <dependency> <groupId ...
- Zookeeper协调服务系统·ELK日志管理系统简介
Zookeeper协调服务系统: 说明:它分布式系统中的协调服务系统,是Hadoop下的一个子项目,可提供的服务有:名字服务.配置服务.分布式同步.组服务等. 3个角色:Leaders.Follow. ...
- 源码级强力分析hadoop的RPC机制
分析对象: hadoop版本:hadoop 0.20.203.0 必备技术点: 1. 动态代理(参考 :http://weixiaolu.iteye.com/blog/1477774 )2. Java ...
- Contest - 中南大学第六届大学生程序设计竞赛(Semilive)
题1:1160十进制-十六进制 注意他给的数据范围 2^31,int是 2^31-1 #include<iostream> using namespace std; int main() ...
- MVC中Spring.net 对基类控制器无效 过滤器控制器无效
比如现在我又一个BaseController作为基类控制器,用于过滤权限.登录判断等作用,其它控制由原本的继承Controller,改为继承BaseController.然后BaseControlle ...
- Redis实现之复制(二)
PSYNC命令的实现 在Redis实现之复制(一)这一章中,我们介绍了PSYNC命令和它的工作机制,但一直没有说明PSYNC命令的参数以及返回值.现在,我们了解了运行ID.复制偏移量.复制积压缓冲区以 ...
- loj2065 「SDOI2016」模式字符串
ref不是太懂 #include <iostream> #include <cstring> #include <cstdio> using namespace s ...
- 【N-Queens】cpp
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- 【Combinations】cpp
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...