162 Find Peak Element 寻找峰值
峰值元素是指其值大于左右相邻值的元素。
给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引。
数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以。
你可以想象得到 num[-1] = num[n] = -∞。
例如,在数组 [1, 2, 3, 1]中 3 是峰值元素您的函数应该返回索引号2。
注意:
你的解决方案应该是对数复杂度的。
详见:https://leetcode.com/problems/find-peak-element/description/
Java实现:
class Solution {
public int findPeakElement(int[] nums) {
int n=nums.length;
if(n==0){
return -1;
}
int left=0;
int right=n-1;
while(left<right){
int mid=(left+right)>>1;
if(nums[mid]<nums[mid+1]){
left=mid+1;
}else{
right=mid;
}
}
return right;
}
}
162 Find Peak Element 寻找峰值的更多相关文章
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- lintcode : find peak element 寻找峰值
题目 寻找峰值 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足 ...
- Leetcode162. Find Peak Element寻找峰值
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2: 或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- 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 --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 【刷题-LeetCode】162 Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- 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] ≠ ...
随机推荐
- luogu2398 SUM GCD
题目大意:求sum i(1->n) (sum j(1->n) (gcd(i,j))). 对于每对(i,j)都来一次gcd很慢,但是我们知道,一个约数i在1~n范围内是n/i个数的约数.gc ...
- hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...
- (linux)schedule_delayed_work()
原文地址:schedule_delayed_work()用法作者:Valley 第一篇 工作队列 在Linux内核中,对下半部(或者说推后执行的工作)的处理方式有好几种,包括BH( ...
- hrtimer高精度定时器的简单使用【学习笔记】
#include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...
- POJ 2017 Speed Limit (直叙式的简单模拟 编程题目 动态属性很少,难度小)
Sp ...
- topcoder 的一些输入输出格式
自从上年的11月份参加过TC的比赛后,就再也没有参加了,因为它的输入输出格式比较难接受,还有它的页面字体比较小,看得我很辛苦...藉口藉口--懒而已!不过以后我会尽量去参加的,为了提高自己的编程能力. ...
- 并不对劲的bzoj3994:loj2185:p3327[SDOI2015]约数个数和
题目大意 设d(x)为x的约数个数,\(t\)组询问,给定\(n,m\)(\(t,m,n\leq5*10^4\)),求$ \sum^n_{i=1}\sum^m_{j=1}d(i*j)$ 题解 假设\( ...
- 安卓开发eclipse如何导出项目
安卓开发如何导出项目 方法/步骤 1 首先打开eclipse 2 选择file然后选择export 3 选择android application 4 点击next 5 选择项目browse可以更 ...
- 平衡二叉树、B树、B+树、B*树、LSM树简介
平衡二叉树是基于分治思想采用二分法的策略提高数据查找速度的二叉树结构.非叶子结点最多只能有两个子结点,且左边子结点点小于当前结点值,右边子结点大于当前结点树,并且为保证查询性能增增删结点时要保证左右两 ...
- nodejs URL 详解
1 我们可以使用.parse方法来将一个URL字符串转换为URL对象 例如: url.parse('http://user:pass@host.com:8080/p/a/t/h?query=strin ...