LeetCode 162. Find Peak Element (找到峰值)
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.
Your solution should be in logarithmic complexity.
题目标签:Array, Binary Search
Java Solution:
Runtime beats 31.71%
完成日期:08/31/2017
关键词:Array, Binary search,
关键点:4种可能性来判断峰值的位置
class Solution
{
public int findPeakElement(int[] nums)
{
int left = 0;
int right = nums.length -1; while(left < right - 1)
{
int mid = left + (right - left) / 2;
// if find peak number
if(nums[mid-1] < nums[mid] && nums[mid] > nums[mid+1])
return mid;
else if(nums[mid-1] < nums[mid] && nums[mid] < nums[mid+1])
{ // if find ascending three numbers, the peak number must be in [mid+1, n-1]
left = mid + 1;
}
else if(nums[mid-1] > nums[mid] && nums[mid] > nums[mid+1])
{ // if find descending three numbers, the peak number must be in [0, mid-1]
right = mid - 1;
}
else if(nums[mid-1] > nums[mid] && nums[mid] < nums[mid+1])
{ // if find the mid number is smaller than both neighbors, meaning both sides have peak number
right = mid - 1;
}
} return nums[left] > nums[right]? left:right;
}
}
参考资料:
https://discuss.leetcode.com/topic/5848/o-logn-solution-javacode/14
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 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] ≠ ...
- 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 --------- 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 && 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(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- 162 Find Peak Element 寻找峰值
峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...
- 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(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
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
随机推荐
- 磁盘管理之inode与block
索引式文件系统 什么是inode? Inode其实就是索引号,便于我们寻找我们文件所存储的数据块block,索引式文件系统在查找信息,读写操作上都比原来的文件系统要快,我们可以通过inode中记录的b ...
- webservice第二篇【自定义webservice服务、soa、uddi概念、soap协议】
自定义webservice服务 我们在上一章节中已经使用wsimport生成本地代理来调用webservice的服务了,其实我们自己写的web应用程序也是可以发布webservice的 我们发布了we ...
- Struts框架2
1.框架:是一个半成品,可以在其基础上在次开发. 2.struts2框架:它是一个web层使用的mvc框架. 3.struts2核心 1.struts2核心 2.xwork核心 4.struts2入门 ...
- Opengl4.5 中文手册—A
因为opengl API 比较庞大,网络上还没有完整的.较新的opengl中文手册 这对很多人很不方便,所以整理了这一系列,用于帮助大家"快速浏览最新的opengl api" 为了 ...
- Redis介绍——Linux环境Redis安装全过程和遇到的问题及解决方案
一:redis的入门介绍: 首先贴出官网; 英文:https://redis.io/ 中文:http://www.redis.cn/ 1.是什么 --REmote DIctionary Server( ...
- mui框架移动开发初体验
前 言 博主最近在接触移动APP,学习了几个小技巧,和大家分享一下. 1. 状态栏设置 现在打开绝大多数APP,状态栏都是与APP一体,不仅美观,而且与整体协调.博主是个中度强迫症患者,顶部那个 ...
- 基于nginx的虚拟主机的配置
安装pcre tar -xvf pcre-8.32.tar.gz cd pcre-8.32 ./configure make;make install 安装nginx 首先创建一个nginx用户,以n ...
- 关于修改extmail附件大小限制的位置
一.修改extmail的webmail.cf文件, SYS_MESSAGE_SIZE_LIMIT = 5242880 注意:以位为单位为5M字节. SYS_MESSAGE_SIZE_LIMIT = x ...
- windows访问控制列表 --ACL(Access Control List)
1.定义 ACL是一个windows中的表示用户(组)权限的列表. Access Control List(ACL) Access Control Entry(ACE) ... 2.分类 ACL分为两 ...
- 跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
问题 生成缩略图 生成验证码 生成二维码 给图片加水印 外部引用 Node 不解释 https://nodejs.org/en/download/ sharp 高性能缩略图 https://gi ...