162. Find Peak Element (Array; Divide-and-Conquer)
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.
思路:log time,所以用二分法。二分法无非是左值、中间值、右值的比较。
如果mid<mid-1,那么下一次判断left~mid-1;(此时mid相当于-∞
)
如果mid<mid+1,那么下一次判断mid+1~right(此时mid相当于-∞
)
class Solution {
public:
int findPeakElement(vector<int>& nums) {
return binarySearch(nums,,nums.size()-);
} int binarySearch(vector<int>& nums, int start, int end){
if(start == end) return start;
if(start+ == end){
if(nums[start]>nums[end]) return start;
else return end;
} int mid = start + (end-start)/;
if(nums[mid]<nums[mid-]){
return binarySearch(nums,start,mid-);
}
else if(nums[mid]<nums[mid+]){
return binarySearch(nums,mid+,end);
}
else{
return mid;
}
}
};
162. Find Peak Element (Array; Divide-and-Conquer)的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 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] ≠ ...
- 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 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 ...
- Find Peak Element(ARRAY - Devide-and-Conquer)
QUESTION A peak element is an element that is greater than its neighbors. Given an input array where ...
随机推荐
- <基础> PHP 数组操作
array_filter — 用回调函数过滤数组中的单元 ( 如果 callback 函数返回 true,则 array 数组的当前值会被包含在返回的结果数组中.数组的键名保留不变 ) array a ...
- 【转】Django 图表制作(By Highcharts)
马克,待不时之需 Django 图表制作(By Highcharts):https://blog.csdn.net/Temanm/article/details/54141759 免费而优秀的图表JS ...
- 使用Prometheus+Grafana监控MySQL实践
一.介绍Prometheus Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采 ...
- 代码直连指定ip的dubbo服务
http://blog.csdn.net/buqutianya/article/details/69229384
- 企业应用--web环境部署于上线流程
服务器逻辑 1.服务器: 2.操作系统: 3.部署逻辑: 测试环境部署 预发布系统: 线上业务服务器部署 业务环境部署逻辑 测试: 上线:
- django403错误(转)
原文:http://blog.sina.com.cn/s/blog_60ccc6e101011ku0.html 处理过程 1.按提示及google结果修改setting.py,在MIDDLEWARE_ ...
- sql中优化查询
1.在大部分情况下,where条件语句中包含or.not,SQL将不使用索引:可以用in代替or,用比较运算符!=代替not. 2.在没有必要显示不重复运行时,不使用distinct关键字,避免增加处 ...
- linux 3.10 的又一次hung
最近又遇到一次hung,dmesg中堆栈如下: [176223.913270] ------------[ cut here ]------------ [ PID: at net/sched/sch ...
- Java设置运行时环境参数
一.代码中,如下: System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", " ...
- openvas开放式漏洞评估系统
OpenVAS是开放式漏洞评估系统,也可以说它是一个包含着相关工具的网络扫描器.其核心部件是一个服务器,包括一套网络漏洞测试程序,可以检测远程系统和应用程序中的安全问题. 用户需要一种自动测试的方法, ...