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[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.
Note:
Your solution should be in logarithmic complexity.
思路:
1.查找,时间复杂度O(logn)使用二分法。
2.在如下图所示情况下,有极大值:

3.长度为1时,由于nums[-1]和nums[n]为负无穷,故0即为极大值下标。
4.根据nums[mid]不同情况分析:
(1)nums[mid]比两边数大,mid即为极大值下标。

(2)nums[mid]比左边大,比右边小,极大值可能在mid右侧,故start= mid。

(3)nums[mid]比右边大,比左边小,极大值可能在mid左侧,故end=mid。

(4)nums[mid]比两边都小,根据nums[end]与nums[end-1]判断。

代码:
public class Solution {
public int findPeakElement(int[] nums) {
int start = 0, end = nums.length-1,mid = 0;
if(nums.length == 1){
return 0;
}
while(start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]){
return mid;
}else if(nums[mid] < nums[mid+1] && nums[mid] > nums[mid-1]){
start = mid;
}else if(nums[mid] > nums[mid+1] && nums[mid] < nums[mid-1]){
end = mid;
}else if(nums[end-1] > nums[end]){
start = mid;
}else{
end = mid;
}
}
if(nums[start] > nums[end]){
return start;
}
return end;
}
}
LeetCode 162.Find Peak Element(M)(P)的更多相关文章
- 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 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 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(二分法)
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/ ...
随机推荐
- 关于guava实现线程池
private ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCac ...
- kettle的基本使用
一.下载下载kettlehttp://sourceforge.net/projects/pentaho/files/Data%20Integration/7.0/pdi-ce-7.0.0.0-25.z ...
- easyui自学总结
1.可拖动 - Draggable 创建一个拖拽元素标记. <div id="dd" class="easyui-draggable" data-opti ...
- Java开发面试常见问题合集
次面试事故 面试官:你看过哪些源码?我:都挺熟悉的面试官:对hashMap了解程度怎么样?面试官:那你能讲讲 HashMap的实现原理吗?面试官:HashMap什么时候会进行 rehash?面试官:结 ...
- VS2010发布,IIS实际目录,无法修改只读状态解难决办法
VS2010发布网站后,无法修改只读状态 CMS简单的主页生成失败,其他的修改操错也应该无法执行 只在常规里修改无效. 网上得答案 1.鼠标右键点击文件夹 2.点击属性 3.在“常规”标签页中,取消“ ...
- 吴裕雄--python编程:CGI编程
什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户 ...
- Dockerfile创建zabbix监控体系
使用for循环将zabbix的镜像导入到容器中 for n in `ls *.tar.gz`;do docker load -i $n ;done 使用docker运行zabbix-server do ...
- 实现 add()(1,2)(3,4)(7,8,9)()
function add(){ var sum=0; function inner(pre,cur){ return pre+cur; } sum=Array.prototype.slice.call ...
- MyBatis之ResultMap的association和collection标签(一)
1.先说resultMap比较容易混淆的点, 2. Map结尾是映射,Type是类型 resultType 和restltMap restulyType: 1.对应的是java对象中的属性,大小写不 ...
- Acwing 844.裸迷宫
给定一个n*m的二维整数数组,用来表示一个迷宫,数组中只包含0或1,其中0表示可以走的路,1表示不可通过的墙壁. 最初,有一个人位于左上角(1, 1)处,已知该人每次可以向上.下.左.右任意一个方向移 ...