峰值元素是指其值大于左右相邻值的元素。
给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引。
数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以。
你可以想象得到  num[-1] = num[n] = -∞。
例如,在数组 [1, 2, 3, 1]中 3 是峰值元素您的函数应该返回索引号2。
注意:
你的解决方案应该是对数复杂度的。

详见:https://leetcode.com/problems/find-peak-element/description/

Java实现:

  1. class Solution {
  2. public int findPeakElement(int[] nums) {
  3. int n=nums.length;
  4. if(n==0){
  5. return -1;
  6. }
  7. int left=0;
  8. int right=n-1;
  9. while(left<right){
  10. int mid=(left+right)>>1;
  11. if(nums[mid]<nums[mid+1]){
  12. left=mid+1;
  13. }else{
  14. right=mid;
  15. }
  16. }
  17. return right;
  18. }
  19. }

162 Find Peak Element 寻找峰值的更多相关文章

  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. lintcode : find peak element 寻找峰值

    题目 寻找峰值 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足 ...

  3. Leetcode162. Find Peak Element寻找峰值

    示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2:   或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...

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

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

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

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  8. LeetCode 162 Find Peak Element

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

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

随机推荐

  1. linux驱动开发之九鼎板载蜂鸣器驱动测试【转】

    本文转载自:http://whylinux.blog.51cto.com/10900429/1932491 字符设备驱动用的fileopretion结构体. 1.板载蜂鸣器的驱动测试 我手里有一个BS ...

  2. codeforces C. Magic Formulas 解题报告

    题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...

  3. Pycharm中如何安装python库

    1首先打开pycharm工具,选择File中的Setting选项,如下图所示 2在打开的setting界面中我们点击python的解释器,你会看到很多导入的第三方库,如下图所示,点击最右边的加号 3在 ...

  4. trying to draw too large(106,975,232 bytes) bitmap.

    Loading Large Bitmaps Efficiently This lesson teaches you to Read Bitmap Dimensions and Type Load a ...

  5. 书写优雅的shell脚本(插曲)- /proc/${pid}/status

    Linux中/proc/[pid]/status详细说明 博客分类: OS Linux多线程  [root@localhost ~]# cat /proc/self/status  Name: cat ...

  6. ubuntu下tesseract 4.0安装及参数使用

    tesseract是一个开源的OCR引擎,最初是由惠普公司开发用来作为其平板扫描仪的OCR引擎,2005年惠普将其开源出来,之后google接手负责维护.目前稳定的版本是3.0.4.0版本加入了基 ...

  7. POJ1474:Video Surveillance(求多边形的核)(占位)

    A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...

  8. liunx命令之【查看某个端口号的使用情况】

    第一:查看端口占用情况的命令:lsof -i:<端口号>

  9. JAVA 布局控制

    在Java里该方法是安一个组件到一个窗体中去,它不同我们使用过的其它GUI系统.首先,它是全代码的:没有控制安放组件的“资源”.其次,该方法的组件被安放到一个被“布局管理器”控制的窗体中,由“布局管理 ...

  10. View Controller Programming Guide for iOS---(三)---Using View Controllers in Your App

    Using View Controllers in Your App Whether you are working with view controllers provided by iOS, or ...