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.

找出峰值。

1、淳朴做法。

public class Solution {
public int findPeakElement(int[] nums) {
int len = nums.length; if( len ==1 || nums[0] > nums[1] )
return 0;
if( nums[len-2] < nums[len-1])
return len-1;
for( int i = 1 ; i < len-1 ; i++ ){ if( nums[i] > nums[i-1] && nums[i] > nums[i+1] )
return i; if( nums[i] > nums[i+1] )
i++;
}
return 0;
}
}

2、还可以使用二分查找。(最佳)

因为从min到max的这个路径中,一定存在一个峰值。

public class Solution {
public int findPeakElement(int[] nums) {
for(int min = 0, max = nums.length -1, mid = max / 2 ; min < max ; mid = (min + max) / 2){
if(min == mid) return nums[min] < nums[max] ? max : min;
min = nums[mid] < nums[mid+1] ? mid : min;
max = nums[mid] > nums[mid+1] ? mid : max;
}
return 0;
}
}

✡ leetcode 162. Find Peak Element --------- java的更多相关文章

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

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

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

  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 && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  5. LeetCode 162 Find Peak Element

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

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

    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(M)(P)

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  8. leetcode 日记 162. Find Peak Element java python

    根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个 ...

  9. LeetCode——162. Find Peak Element

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

随机推荐

  1. ARC 类型转换:显式转换 id 和 void *

    http://blog.csdn.net/chinahaerbin/article/details/9471419 /* * ARC有效时三种类型转换: */ 1.__bridge           ...

  2. C# 调用配置文件SQL语句 真2B!

    /********************************************************************************* ** File Name : SQ ...

  3. exec方法

    如果 exec 方法没有找到匹配,将返回 null.如果找到匹配项,则 exec 方法返回一个数组,并将更新全局 RegExp 对象的属性以反映匹配结果.数组元素 0 包含了完整的匹配项,而元素 1 ...

  4. web api post传一个参数时 值永远是null

    这个问题纠结了我一个早上,不管用什么样的传参方法,走到控制器中,那个参数永远不变的等于null 在网上找了很多解决方案 上面这个是从网上截图的,第一:要将参数标记为[FromBody],变为简单参数 ...

  5. 云计算平台简介(App Engine)

    云计算平台简介(App Engine)     1   简介 App Engine: 应用程序引擎,是托管网络应用程序的云计算平台. 1.1  什么是云 云计算通常简称为“云”,是一种通过 Inter ...

  6. VS2010编译Qt5.4.0静态库

    http://www.kavenblog.com/?p=375 1.Qt的跨平台十分优秀,但是在Windows上是还是会有许多问题,其中之一就是动态链接库的问题,Qt程序的发布必须带一个体积不小的DL ...

  7. 多数求和(java)

    实验题目:从命令行接受多个数字,求和之后输出结果. 设计思想:命令行输入的字符会赋值给args数组,所以在命令行输入数字后,直接取出args的数组长度,作为循环语句的终点判断,然后利用循环将字符型改为 ...

  8. python 优雅的使用正则表达式 ~ 1

    正则表达式简介 正则表达式 , 也称谓 REs , 本质上是一个微小且高度专业化的编程语言. 他被嵌入到许多语言当中 , 例如 python 就是通过 re 模块来提供给我们使用 , 正则表达式 是通 ...

  9. CGI标准简介 ~ Django

    CGI CGI(Common Gateway Interface)是WWW技术中最重要的技术之一 , 有着不可替代的重要地位 , CGI是外部应用程序(CGI程序)于Web服务器之间的接口标准 , 实 ...

  10. HTML的表单

    HTML表单 <!-- <form></form>标签对用来创建一个表单,即定义表单的开始和结束位置,<form>表单具有下面等属性 1.action属性用来 ...