题目:

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)的更多相关文章

  1. LeetCode 162. Find Peak Element (找到峰值)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. (二分查找 拓展) 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 ...

  3. [LeetCode] 162. Find Peak Element 查找峰值元素

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  4. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  5. 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] ≠ ...

  6. ✡ 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] ≠ ...

  7. leetcode 162 Find Peak Element(二分法)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. LeetCode——162. Find Peak Element

    一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...

  9. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

随机推荐

  1. 比率(ratio)|帕雷托图|雷达图|轮廓图|条形图|茎叶图|直方图|线图|折线图|间隔数据|比例数据|标准分数|标准差系数|离散系数|平均差|异众比率|四分位差|切比雪夫|右偏分布|

    比率是什么? 比率(ratio) :不同类别数值的比值 在中文里,比率这个词被用来代表两个数量的比值,这包括了两个相似却在用法上有所区分的概念:一个是比的值:另一是变化率,是一个数量相对于另一数量的变 ...

  2. vs编译完提示不支持尝试的执行操作

    最近公司在用MFC做项目,编译完就弹窗提示,不支持尝试的执行操作,开始没注意,但是点击弹窗提示无反应: 问题原因:dialog里的控件有的已经删除了,但是DoDataExchange函数还存在控件的关 ...

  3. LeetCode No.79,80,81

    No.79 Exist 单词搜索 题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相 ...

  4. FastDFS安装部署

    博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽 服务器信息: Storage:192.168.247.20 Traker:192.168.247.21 一.搭建环境准备 ...

  5. css3动画贝塞尔曲线cubic-bezier,css3动画的五种情况

    当大家开始做css3动画的时候,了解贝塞尔曲线就成了不可或缺的.“贝赛尔曲线”是由法国数学家Pierre Bézier所发明,由此为计算机矢量图形学奠定了基础.它的主要意义在于无论是直线或曲线都能在数 ...

  6. mysql索引详细介绍

    博客: https://blog.csdn.net/tongdanping/article/details/79878302#%E4%B8%89%E3%80%81%E7%B4%A2%E5%BC%95% ...

  7. 化学键|甘氨酸|谷氨酸|半胱胺酸|motif|domain|疏水相互作用|序列相似性|clustering analysis|Chou and Fasman|GOR|PHD|穿线法|first-principle ab initio folding|

    化学键|甘氨酸|谷氨酸|半胱胺酸|motif|domain|疏水相互作用|序列相似性|clustering analysis|Chou and Fasman|GOR|PHD|穿线法|first-pri ...

  8. 吴裕雄--天生自然python学习笔记:Python3 多线程

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进度条 ...

  9. springboot项目基础面试题

    1.springboot与spring的区别. 引用自官方说法: java在集成spring等框架需要作出大量的配置,开发效率低,繁琐.所以官方提出 spring boot的核心思想:习惯优于配置.可 ...

  10. python jQuery筛选器

    筛选器:$(this).next() 下一个    $(this).prev  上一个    $(this).parent()  父     $(this).children() 孩     $(th ...