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. fqrouter让安卓手机登陆facebook成为可能

    大多数人向来都是在电脑上通过各种代理工具来访问一些国外网站,例如facebook,twitter,然而你是否想过可以通过你的手机来畅游这些网站呢,接下来我将介绍一种通过fqrouer实现使用安卓手机畅 ...

  2. java udp网络编程

    import java.net.*; /* 通过UDP传输发送文字数据 1.建立socket服务 2.提供数据,并封装到数据包中 3.通过sokect服务的发送功能,将数据包发送出去 4.关闭资源 * ...

  3. C语言与MATLAB接口 编程与实例 李传军编着

    罗列一下以前自己学习C语言与MATLAB混编的笔记,顺便复习一遍. <C语言与MATLAB接口 编程与实例 李传军编着>(未看完,目前看到P106) 目录P4-8 ************ ...

  4. iOS 获取当前月份的天数(转)

    在这里我很鄙视百度,尼玛 竟然每一个我想要的结果...最后还是用google弄到的.日前又需要自己以后慢慢研究 1. 获取当前月份有多少天 NSCalendar *calendar = [NSCale ...

  5. Linux下GCC的使用

    1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...

  6. 简单的php Mysql类(查询 删除 更新)

    php Mysql类一般都包括了几乎我们常用的数据库操作方法,这里只提供了查询 删除 更新三种操作,算不是很全只是一个简单的数据库查询类了.      代码如下 复制代码 class mysql { ...

  7. stm32启动文件 startup_stm32f10x_hd.s

    ;* 文件名          : startup_stm32f10x_hd.s;* 库版本           : V3.5.0;* 说明:             此文件为STM32F10x高密度 ...

  8. 实用的WPF Xml的简易读写类以及用法示例

    转自:http://www.silverlightchina.net/html/study/WPF/2012/0808/17980.html 最近真是写博客写的不可收拾,今天再来一篇. 因为做一些程序 ...

  9. Ogre骨骼动画

    转自:http://blog.csdn.net/yanonsoftware/article/details/1281516 OGRE的基本动画控制是很简单的,设置一个动画的操作是这样: // Set ...

  10. Ubuntu 14.10 下awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...