✡ 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] ≠ 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的更多相关文章
- 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 (找到峰值)
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 && 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
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- 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(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- leetcode 日记 162. Find Peak Element java python
根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个 ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
随机推荐
- Rhel6-heartbeat配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...
- php变量的判空和类型判断
(1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...
- Linux下备份系统至另一硬盘
首先会想到dd命令. 但,, 1,若是小硬盘还好,上T的大硬盘这样做肯定不明智; 2,况且dd是在硬件层面的拷贝,前面的MBR也会随之恢复到另一个盘,若源硬盘是100G,目标盘是200G,又会出问题, ...
- JS随鼠标坐标移动,显示浮动层内容
在表单等项目中往往会遇到类似于“备注”.“说明”等100个字内的内容需要显示. 这些内容如果全部呈现开,会影响布局和美观,确又没有必要设计一个层或是一个页面. 那么,我们可以把这些内容放到浮动层中,鼠 ...
- 常错-UIScrollView中得图片不能被拖动
经常在开发中,发现自己UIScrollView里面的图片不能被拖动 在这里做个备忘,第一种可能是contentSize没有设置,第二种可能是contentSize设置的太小了. 测试后发现,与user ...
- 修改Oracle数据库的字符集为UTF-8
1.改客户端字符集:通过WINDOWS的运行菜单运行Regedit,修改注册表 Start -> Run -> Rededit <-| Under registry Editor - ...
- 【django入门教程】Django的安装和入门
很多初学django的朋友,都不知道如何安装django开发以及django的入门,今天小编就给大家讲讲django入门教程. 注明:python版本为3.3.1.Django版本为1.5.1,操作系 ...
- 华为V-ISA信誉安全体系:对付新型DDoS攻击的利器
华为Anti-DDoS解决方案基于华为颇具传统优势的专业软硬件平台开发,在防护机制中,引入先进的检测机制,提供了业内首创的“V-ISA”信誉安全体系,是业界唯一单机可提供超百G DDoS防御能 ...
- bind,call,apply区别
js中bind.call.apply函数的用法 2015-02-27 21:16:39 标签:javascript js bind call apply 原创作品,允许转载,转载时请务必以超链接形式 ...
- 极客DIY:廉价电视棒玩转GNSS-SDR,实现GPS实时定位
0×00 前言 GNSS是Global Navigation Satellite System的缩写.中文称作:全球卫星导航系统.全球导航卫星系统. GNSS泛指所有的卫星导航系统,包括全球的.区域的 ...