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 ...
随机推荐
- Matlab Delete Row or Col 删除矩阵的行或列
Matlab中,我们有时候要删除矩阵中的某行某列,可以采用下列方法进行删除: a = [ ]; a(,:) = []; % Delete row a(:,) = []; % Delete col
- java 面向对象--------时间作业
编写Java应用程序.首先,定义一个时钟类——Clock,它包括三个int型 成员变量分别表示时.分.秒,一个构造方法用于对三个成员变量(时.分.秒) 进行初始化,还有一个成员方法show()用于显示 ...
- 一些Office 365的问题收集
1. 按照MS的最佳实践, 应该是先有本地域, 然后再有Office 365. 但是我们公司刚好相反, 于是按照前面的文章做完了硬关联, 但是现在发现对于那些原本就在Office 365上的用户的Pr ...
- HDU 1671 Phone List(Trie的应用与内存释放)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 前端编码风格规范(3)—— JavaScript 规范
JavaScript 规范 全局命名空间污染与 IIFE 总是将代码包裹成一个 IIFE(Immediately-Invoked Function Expression),用以创建独立隔绝的定义域.这 ...
- Apache Spark源码走读之6 -- 存储子系统分析
欢迎转载,转载请注明出处,徽沪一郎. 楔子 Spark计算速度远胜于Hadoop的原因之一就在于中间结果是缓存在内存而不是直接写入到disk,本文尝试分析Spark中存储子系统的构成,并以数据写入和数 ...
- 类型强转(type cast)
类型转换有 c 风格的,当然还有 c++风格的.c 风格的转换的格式很简单(TYPEEXPRESSION),但是 c 风格的类型转换有不少的缺点,有的时候用 c 风格的转换是不合适的, 因为它可以在任 ...
- RedirectResult,RedirectToRoute
RedirectResult:运行重新导向到其他网址,在RedirectResult的内部,基本上还是以Response.Redirect方法响应HTTP 302暂时导向. eg: public Ac ...
- docker安装与启动
安装docker [root@localhost /]# yum -y install docker-io 更改配置文件 [root@localhost /]# vi /etc/sysconf ...
- php session 跨页失效问题
原因是session.savepath 目录不存在或者没有读写权限