【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 ...
随机推荐
- 【c语言】将正数变成相应的负数,将负数变成相应的正数
<pre name="code" class="cpp">// 将正数变成相应的负数,将负数变成相应的正数 #include <stdio.h ...
- 为什么需要标准IO缓冲?
(转)标准I/O缓冲:全缓冲.行缓冲.无缓冲 标准I/O库提供缓冲的目的是尽可能地减少使用read和write调用的次数.它也对每个I/O流自动地进行缓冲管理,从而避免了应用程序需要考虑这一点所带来的 ...
- win7系统
网址:http://www.xitongma.com/Windows7/ 使用方法:http://www.cnblogs.com/henrychan688/p/5223935.html
- 拦截器(Inteceptor),过滤器(Filter),切面(Aspect)处理HttpServiceReqeust请求
1.拦截器 java里的拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可 ...
- Python标准库:内置函数delattr(object, name)
本函数是用来删除对象的属性,比方在函数setattr()里加入的属性,就能够利用这个函数来删除. 參数object是一个对象,參数name是一个字符串,但这个字符串必须是对象的属性.比方delattr ...
- SharePoint 2013/2010 在一个列表或文档库内移动列表项,文档和目录位置而保持last modify by 等系统字段保持不变
本文讲述SharePoint 2013/2010 在一个列表或文档库内移动列表项.文档和目录位置而保持last modify by 等系统字段保持不变的解决方式. 近期遇到客户一个需求,在一个列表或文 ...
- Java中匿名内部类
匿名内部类也就是没有名字的内部类 正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写 但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口 实例1:不使用匿名内部类来实现抽象 ...
- [HEOI2015]兔子与樱花[贪心]
4027: [HEOI2015]兔子与樱花 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1043 Solved: 598[Submit][Stat ...
- 【BZOJ1996】[Hnoi2010]chorus 合唱队 区间DP
[BZOJ1996][Hnoi2010]chorus 合唱队 Description Input Output Sample Input 4 1701 1702 1703 1704 Sample Ou ...
- 【BZOJ1570】[JSOI2008]Blue Mary的旅行 动态加边网络流
[BZOJ1570][JSOI2008]Blue Mary的旅行 Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决 ...