75. Find Peak Element 【medium】
75. Find Peak Element 【medium】
There is an integer array which has the following features:
- The numbers in adjacent positions are different.
- A[0] < A[1] && A[A.length - 2] > A[A.length - 1].
We define a position P is a peek if:
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in this array. Return the index of the peak.
Notice
- It's guaranteed the array has at least one peak.
- The array may contain multiple peeks, find any of them.
- The array has at least 3 numbers in it.
Given [1, 2, 1, 3, 4, 5, 7, 6]
Return index 1 (which is number 2) or 6 (which is number 7)
Time complexity O(logN)
解法一:
class Solution {
public:
/*
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> &A) {
if (A.empty()) {
return -;
}
int start = ;
int end = A.size() - ;
while (start + < end) {
int mid = start + (end - start) / ;
if (A[mid] > A[mid - ]) {
if (A[mid] > A[mid + ]) {
return mid;
}
else {
start = mid;
}
}
else {
if (A[mid] > A[mid + ]) {
end = mid;
}
else {
start = mid;
}
}
}
return -;
}
};
分类讨论。
解法二:
class Solution {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public int findPeak(int[] A) {
// write your code here
int start = , end = A.length-; // 1.答案在之间,2.不会出界
while(start + < end) {
int mid = (start + end) / ;
if(A[mid] < A[mid - ]) {
end = mid;
} else if(A[mid] < A[mid + ]) {
start = mid;
} else {
end = mid;
}
}
if(A[start] < A[end]) {
return end;
} else {
return start;
}
}
}
参考http://www.jiuzhang.com/solution/find-peak-element/的解法,此法更简单。
75. Find Peak Element 【medium】的更多相关文章
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 61. Search for a Range【medium】
61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
随机推荐
- small test on 5.30 night T2
(题面写错了,应该是一条从b -> a 的边) 让我们设状态 (a,b,c) 表示存在一个点k,使得 dist(k,b) - dist(k,a) * 2 + 3 = c,显然这里的第三维可以压 ...
- Python中内置的日志模块logging用法详解
logging模块简介 Python的logging模块提供了通用的日志系统,可以方便第三方模块或者是应用使用.这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP GET/P ...
- Eclipse / Pycharm | 使用过程中的一些问题笔记
最近有比较多的用到这两款工具,其中也遇到一些问题,知道了一些快捷键 快捷键什么的这里就不讲了,去网上搜搜,经常使用下,自然就熟悉了 主要记录一下我遇到的几个问题 文章目录 Pycharm出现的部分快捷 ...
- Scala实战高手****第7课:零基础实战Scala面向对象编程及Spark源码解析
/** * 如果有这些语法的支持,我们说这门语言是支持面向对象的语言 * 其实真正面向对象的精髓是不是封装.继承.多态呢? * --->肯定不是,封装.继承.多态,只不过是支撑面向对象的 * 一 ...
- cssz中<a>标签鼠标选中去除选中边框
IE: <a href="#" hidefocus="true"></a> 非IE: a:focus { outline:none; } ...
- 建立DB-LINK和建立视图
在系统数据通信间经常会有数据库的数据直接引用,使用视图VIEW的方式实现.视图调用通常会有两种情况,一种是同一数据库的视图,一种是跨数据库的视图. 在同一数据库地址不同用户下,不过不同的用户视图调用需 ...
- Solr6.6.0 用 SimplePostTool索引文件的启示
本文主要是介绍通过SimplePostTool工具索引文件的结果进行确认,针对不同的文件,索引的结果不同. 1.创建core 首先启动solr,建立名称为data的core,SimplePostToo ...
- Spark(九) -- SparkSQL API编程
本文测试的Spark版本是1.3.1 Text文本文件测试 一个简单的person.txt文件内容为: JChubby,13 Looky,14 LL,15 分别是Name和Age 在Idea中新建Ob ...
- 插件化注解处理API(Pluggable Annotation Processing API)
Java奇技淫巧-插件化注解处理API(Pluggable Annotation Processing API) 参考资料 JDK6的新特性之六:插入式注解处理API(Pluggable Annota ...
- Linux——配置使用github
前一段时间在windows下配置了github的环境,参考“TortoiseGit连接github.com”一文,现在学习在linux下编程,在网上找了点资料,配置在linux下使用github,将过 ...