【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.
题解:题目要求用O(logn)的时间复杂度找到局部最大值,很容易想到二分算法。设置一个函数 private boolean findPeakHelper(int[] num,int begin,int end){ ,每次判断(begin+end)/2位置上的元素是否满足要求,如果不满足就搜索左半边数组,如果在左半边搜索到了,右半边就没有必要搜索了,因为题目只让返回一个解;如果左半边没搜索到,就继续递归搜索右半边。特别注意对位置0和位置n-1处元素的处理。
JAVA版本代码如下:
public class Solution {
int index = 0;
public int findPeakElement(int[] num) {
findPeakHelper(num, 0, num.length-1);
return index;
}
private boolean findPeakHelper(int[] num,int begin,int end){
if(begin > end)
return false;
int mid = (begin + end)/2;
if(mid == 0)
{
if(mid+1 < num.length && num[mid+1] < num[mid]){
index = mid;
return true;
}else {
return findPeakHelper(num, mid+1, end);
}
}
if(mid-1 >= 0 && mid == num.length-1){
if(num[mid-1] < num[mid]){
index = mid;
return true;
}else{
return findPeakHelper(num, begin, mid-1);
}
}
if(num[mid-1] < num[mid] && num[mid+1] < num[mid]){
index = mid;
return true;
}
if(findPeakHelper(num, begin, mid-1))
return true;
else {
return findPeakHelper(num, mid+1, end);
}
}
}
【leetcode刷题笔记】Find Peak Element的更多相关文章
- 【leetcode刷题笔记】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
- leetcode刷题笔记08 字符串转整数 (atoi)
题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- unity 打开文件夹并鼠标选中某文件
System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "explor ...
- scrapy 相关
Spider类的一些自定制 # Spider类 自定义 起始解析器 def start_requests(self): for url in self.start_urls: yield Reques ...
- VR应用开发遍地走的日子还有多远
从上世纪60年代美国计算机科学家Ivan Sutherland发明的第一款真正意义上的虚拟现实头盔,到Facebook以20亿美元收购"虚拟现实之眼"Oculus Rift,大批厂 ...
- JMeter java.net.URISyntaxException: Illegal character in query at index
请求参数未编码,会造成请求解析失败.把编码勾上,就可以了.
- 【NOI2015】品酒大会[后缀数组]
#131. [NOI2015]品酒大会 统计 描述 提交 自定义测试 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品酒家”和“首席猎手”两个奖项, ...
- VS2013\VS2017 使用git 总是需要输入账号密码
问题: VS2013\VS2017 使用git 总是需要输入账号密码 解决方案:删除原凭证,或者修改原凭证,重新输入一次账号和密码并且选择“记住凭证”即可!
- mysql的增量备份与全备的脚本
mysql全量备份.增量备份.开启mysql的logbin日志功能.在/etc/my.cnf文件中加入以下代码: [mysqld]log-bin = "/home/mysql/logbin. ...
- kafka_2.11-0.10.1.1集群搭建安装配置
在搭建kafka集群之前,请保证zookeeper已安装. 1.下载 官网下载链接:http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/0.10.1.1/ ...
- android问题总结
1.当打开eclipse时出现如下窗口(内容如下) Error when loading the SDK: Error: Error parsing \Android\adt-bundle-windo ...
- Spring4学习笔记-AOP(基于配置文件的方式)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...