LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/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.
题解:
二分法, 如何判断接下来应该找左边还是右边呢。依据其实是比较nums[m] 和 nums[m+1], 若是nums[m] < nums[m+1], peak 一定会出现在mid 右边,不包括mid.
反之peak 就会出现在mid 左边, 但要包括mid.
Time Complexity: O(logn). Space: O(1).
AC Java:
class Solution {
public int findPeakElement(int[] nums) {
if(nums == null || nums.length == 0){
return -1;
} int l = 0;
int r = nums.length - 1;
while(l < r){
int mid = l + (r-l)/2;
if(nums[mid] < nums[mid+1]){
l = mid+1;
}else{
r = mid;
}
} return r;
}
}
类似Peak Index in a Mountain Array.
LeetCode Find Peak Element的更多相关文章
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Find Peak Element [TBD]
说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...
- LeetCode Find Peak Element 找临时最大值
Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...
- LeetCode: Find Peak Element 解题报告
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- [LeetCode] 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
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- LeetCode OJ 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 ...
随机推荐
- 在ASP.Net2.0中使用UrlRewritingNet实现链接重写
采用UrlRewritingNet.UrlRewriter.dll来轻松实现UrlRewritingNet.UrlRewriter.dll 可从其官方网站下载:http://www.urlrewrit ...
- 常用的一些webshell木马官方后门
80SEC剑心修改过世界杀毒版 http://wwwxx.cn/1.asp?web2a2dmin=//?web2a2dmin=//1.asp 绕过 不灭之魂的后门 1.在错误页面右键可以查看密码 2 ...
- obout editor Absolute path for uploaded image
本文转自:https://www.obout.com/editor_new/KnowledgeBase.aspx?id=706 Absolute path for uploaded image Q ...
- Task Scheduler Error and Success Constants (Windows)
If an error occurs, the Task Scheduler APIs can return one of the following error codes as an HRESUL ...
- css 细节收集
细节1……………………………………………………………………………… 一.当文字与图片在一行,需要将文字与图片底对齐,需要这样写: <li>记住密码<img src="&qu ...
- [ZZ] [siggraph10]color enhancement and rendering in film and game productio
原文link:<color enhancement and rendering in film and game production> 是siggraph 2010,“Color Enh ...
- visual studio 中使用的插件介绍
Highlight all occurrences of selected word 高亮代码 Indent Guides 代码的开头结尾连接竖线..是代码更清洗 PHP Tools for visu ...
- PHP--进行模块化设计
PHP--进行模块化设计 [来源] 达内 [编辑] 达内 [时间]2012-10-30 导航模块可以简单列为一个关于三级页面链接的HTML文件.通常你可以通过用另一种颜色来标明对当前区域的链 ...
- LVS的DR模式配置
一.基本规划负载均衡调度器 192.168.1.104 默认网关 192.168.1.1 ip别名 192.168.1.233realserver1 192.168 ...
- 如何删除docker images/containers
docker images往往不知不觉就占满了硬盘空间,为了清理冗余的image,可采用以下方法: 1.进入root权限 sudo su 2.停止所有的container,这样才能够删除其中的imag ...