✡ 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的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
随机推荐
- POJ 3237
题目大意:指定一颗树上有3个操作:询问操作,询问a点和b点之间的路径上最长的那条边的长度:取反操作,将a点和b点之间的路径权值都取相反数:变化操作,把某条边的权值变成指定的值. #include &l ...
- jsp中普通按钮如何提交表单
jsp中普通按钮如何提交表单方法1: <form action = "提交的地址"> <input type="submit" ...
- mybatis 的 resulttype 和resultMap
resultType适合返回值比较简单的,比如一个数据类型,或者一个对象.比如对象的情况,是将表的列名和对象的属性一一对应的. 但是resultType无法处理返回值比较复杂的,特别是连接查询,需要用 ...
- MySQL表的增删改查和列的修改(二)
一.使用Like模糊查找搜索前缀为以“exam_”开头的表名 show tables like 'exam_%' ; 语句结束符号是:也是用\G来表示 二.MySQL表的CRUD 2.1 创建表: C ...
- 一个基于atomic的卖票测试
package testAtomic; import java.util.concurrent.atomic.AtomicInteger; import sun.security.krb5.inter ...
- windows防火墙添加规则
#include <windows.h> #include <crtdbg.h> #include <netfw.h> #include <objbase.h ...
- Oracle之ROW_NUMBER() OVER函数
语法:ROW_NUMBER() OVER(ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的select ACD_ID,ROW_NUMBE ...
- 2016 - 1- 22 NSURLConnetction --- POST请求
一:与上一篇博客中的GET方法类似 只不过需要多注意,如果要改变请求的类型,需要生成NSMutableURLRequest对象才可以设置请求的类型. NSURL *url = [NSURL URLW ...
- iOS打包为ipa的两种方式和生成P12证书的方式
iOS项目打包为ipa的两种方式: 准备工作:先行在Xcode里面打开preferences,填写apple id. 通过iTunes+Xcode 在Xcode里,把模拟器调整为iOS Device, ...
- Divisors_组合数因子个数
Description Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- ...