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/ ...
随机推荐
- EmailService
package me.zhengjie.tools.service; import me.zhengjie.tools.domain.EmailConfig; import me.zhengjie.t ...
- python Pandas Profiling 一行代码EDA 探索性数据分析
文章大纲 1. 探索性数据分析 代码样例 效果 解决pandas profile 中文显示的问题 1. 探索性数据分析 数据的筛选.重组.结构化.预处理等都属于探索性数据分析的范畴,探索性数据分析是帮 ...
- mysql在当前服务器复制数据库
mysqldump newwq -u root -proot --add-drop-table | mysql jxg -u root -proot
- JS UTC 昨天
var birthday = new Date("Jan 01, 1983 01:15:00") var formatDate = function (date) { ...
- yield解析
1.yield可以用来为一个函数返回值塞数据 代码: def addlist(alist): for i in alist: alist = [, , , ] for x in addlist(ali ...
- marry|psych up|make it|Fireworks|be to blame for|
同位语从句 ADJ 结婚的;已婚的If you are married, you have a husband or wife. We have been married for 14 years.. ...
- 基于Dockerfile制作tomcat镜像
Docker 概述: 在前面的例子中,我们从下载镜像,启动容器,在容器中输入命令来运行程序,这些命令都是手工一条条往里输入的,无法重复利用,而且效率很低.所以就需要一种文件或脚本,我们把想执行的操 ...
- SpringBoot + JPA + mariadb
SpringBoot + JPA + MariaDB 源码:https://github.com/ldl326308/LiveGreen-SpringBoot.git JPA持久层方法名命名规则:
- Leetcode刷题记录 旋转矩阵
https://leetcode-cn.com/problems/spiral-matrix/submissions/ class Solution(object): def spiralOrder( ...
- 如何用js判断一个对象是不是Array
.如何用js判断一个对象是不是Array 1.Array.isArray(obj) 调用数组的isArray方法 2.obj instanceof Array 判断对象是否是Array的实例 3.Ob ...