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] ≠ ...
随机推荐
- linux驱动开发之九鼎板载蜂鸣器驱动测试【转】
本文转载自:http://whylinux.blog.51cto.com/10900429/1932491 字符设备驱动用的fileopretion结构体. 1.板载蜂鸣器的驱动测试 我手里有一个BS ...
- codeforces C. Magic Formulas 解题报告
题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...
- Pycharm中如何安装python库
1首先打开pycharm工具,选择File中的Setting选项,如下图所示 2在打开的setting界面中我们点击python的解释器,你会看到很多导入的第三方库,如下图所示,点击最右边的加号 3在 ...
- trying to draw too large(106,975,232 bytes) bitmap.
Loading Large Bitmaps Efficiently This lesson teaches you to Read Bitmap Dimensions and Type Load a ...
- 书写优雅的shell脚本(插曲)- /proc/${pid}/status
Linux中/proc/[pid]/status详细说明 博客分类: OS Linux多线程 [root@localhost ~]# cat /proc/self/status Name: cat ...
- ubuntu下tesseract 4.0安装及参数使用
tesseract是一个开源的OCR引擎,最初是由惠普公司开发用来作为其平板扫描仪的OCR引擎,2005年惠普将其开源出来,之后google接手负责维护.目前稳定的版本是3.0.4.0版本加入了基 ...
- POJ1474:Video Surveillance(求多边形的核)(占位)
A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...
- liunx命令之【查看某个端口号的使用情况】
第一:查看端口占用情况的命令:lsof -i:<端口号>
- JAVA 布局控制
在Java里该方法是安一个组件到一个窗体中去,它不同我们使用过的其它GUI系统.首先,它是全代码的:没有控制安放组件的“资源”.其次,该方法的组件被安放到一个被“布局管理器”控制的窗体中,由“布局管理 ...
- View Controller Programming Guide for iOS---(三)---Using View Controllers in Your App
Using View Controllers in Your App Whether you are working with view controllers provided by iOS, or ...