✡ 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的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
随机推荐
- asp.net页面使用doPostBack的后台取值
前台页面(aspx文件): --伪装按钮 <span onclick='__doPostBack("lkSend","key")'>发送</s ...
- C/C++文件结构
总结者:kate (1).h 为头文件:存放 版权和版本声明,预处理块 ,函数和类结构声明 (2).cpp文件:代码文件,存放程序的实现 大都数时候,源文件和头文件是对应出现的,比如有一个A.cpp ...
- [安卓][转]internal(com.android.internal)和hidden(@hide)APIs简介及在应用程序中的调用方法
转自:http://www.cnblogs.com/xirihanlin/archive/2011/06/05/2073118.html [引言]:我在做android softap的时候看到andr ...
- Repeater 合并单元格
前途页面: <asp:Repeater ID="rptList" runat="server" OnPreRender="rptList_Pre ...
- ios上传应用后,审核流程完成前(reveiw)修改了程序内容,如何上传替换
其实挺简单,只需要更改下version和build版本 看图说话就可以.我的程序之前版的版本设置 修改bug之后的设置: 然后重新打包就好了,提示打包成功后,在itunesconnect查看发现 选中 ...
- 计算系数(noip2011)
[问题描述]给定一个多项式(ax + by)^k,请求出多项式展开后(x^n)*(y^m)项的系数.[输入]输入文件名为 factor.in.共一行,包含 5 个整数,分别为a,b,k,n,m,每两个 ...
- 一篇介绍jquery中的ajax的结合
<script type="text/javascript"> function Text_ajax() { $.aja ...
- C#注册表常用操作
1:加键 改值 Microsoft.Win32.RegistryKey Key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey( @" ...
- 关于RecyclerView中Viewholder和View的缓存机制的探究
关于RecyclerView中Viewholder和View的缓存机制的探究 http://www.cnblogs.com/littlepanpc/p/4241575.html
- 2016 - 1- 19 利用多线程优化从网上加载图片的Demo
// // ZZTableViewController.m // 多图片下载 // // Created by Mac on 16/1/19. // Copyright © 2016年 Mac. Al ...