✡ 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的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
随机推荐
- 可以用WMI来获取磁盘及分区编号
{$APPTYPE CONSOLE} uses SysUtils, ActiveX, ComObj, Variants; function ListDrives : string; var FSWbe ...
- C++-前缀和后缀
1,c++规定后缀形式的++操作符有一个int行的参数,被调用时,编译器自动加一个0作为参数给他 2,前缀返回一个reference,后缀返回一个const对象 /////////////////// ...
- hdu 1030 Delta-wave (C++, 0ms, explanatory comments.) 分类: hdoj 2015-06-15 12:21 45人阅读 评论(0) 收藏
problem description http://acm.hdu.edu.cn/showproblem.php?pid=1030 #include <cstdio> #include ...
- 对项目的测试--Resharper
初学 这里做个记录. 1:安装后,Resharper会用他自己的英文智能提示,替换掉 vs2010的智能提示,所以我们要换回到vs2010的智能提示 2:快捷键.是使用vs2010的快捷键还是使用 R ...
- Autolayout-VFL语言添加约束
一.VFL语言简洁 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
- redis 详解
什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) Reids的特点 Redis本质上是一个Key-Value类型的内存数据库, ...
- Xlistview的values下的界面
<!-- 下拉刷新,上拉加载更多 --> <string name="xlistview_header_hint_normal">下拉刷新</s ...
- 北大ACM题库习题分类与简介(转载)
在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...
- xml文件有误
Unable to start activity ComponentInfo{com.anzi.jmsht.scripturelibrary/com.anzi.jmsht.scripturelibra ...
- Python inspect
inspect — Inspect live objects New in version 2.1. The inspect module provides several useful functi ...