【题目】
A peak element is an element that is greater than its neighbors.
Given an input array wherenum[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 thatnum[-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.
click to show spoilers.
Note:
Your solution should be in logarithmic complexity.
【分析一】
我们首先想到的就是时间复杂度为O(n)的顺序遍历,对每一个元素,与它相邻的元素比较。
这样也可以AC。
Your solution should be in logarithmic complexity.
可是题目要求是时间复杂度是对数级别的。
【代码一】
/*********************************
* 日期:2015-01-31
* 作者:SJF0115
* 题目: 162.Find Peak Element
* 网址:https://oj.leetcode.com/problems/find-peak-element/
* 结果:AC
* 来源:LeetCode
* 博客:
**********************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int findPeakElement(const vector<int> &num) {
int size = num.size();
if(size == 1){
return 0;
}//if
// 判断第一个元素和最后一个元素
if(size > 1){
if(num[0] > num[1]){
return 0;
}//if
if(num[size-1] > num[size-2]){
return size-1;
}//if
}//if
for(int i = 1;i < size-1;++i){
if(num[i] > num[i-1] && num[i] > num[i+1]){
return i;
}//if
}//for
}
};
int main(){
Solution solution;
// vector<int> num = {1,2,3,1};
//vector<int> num = {4,3,3,1};
vector<int> num = {1,2,3,4};
int result = solution.findPeakElement(num);
// 输出
cout<<result<<endl;
return 0;
}
【分析二】
根据给出的条件: num[i] != num[i+1], 相邻两个元素不相等。运用二分查找原理。
(1)如果 num[i-1] < num[i] > num[i+1], 则num[i] 就是 peak
(2)如果 num[i-1] < num[i] < num[i+1], 则 num[i+1...n-1] 必定包含一个 peak
(3)如果 num[i-1] > num[i] > num[i+1], 则num[0...i-1]
必定包含一个 peak
(4)如果 num[i-1] > num[i] < num[i+1], 则 两边都有一个 peak
继续优化一下,通过上面仔细观察一下:
(1)如果 num[i-1] < num[i] > num[i+1], 则num[i] 就是 peak
(2)如果 num[i-1] < num[i] , 则 num[i+1...n-1] 必定包含一个 peak,left指向mid+1
(3)如果 num[i-1] > num[i] , 则num[0...i-1]必定包含一个 peak,right指向mid-1
【代码二】
/*------------------------------------
* 日期:2015-01-31
* 作者:SJF0115
* 题目: 162.Find Peak Element
* 网址:https://oj.leetcode.com/problems/find-peak-element/
* 结果:AC
* 来源:LeetCode
* 博客:
---------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int findPeakElement(const vector<int> &num) {
int size = num.size();
// only one element
if (size == 1) {
return 0;
}//if
int left = 0, right = size - 1;
int mid;
// left right 距离为1 退出
while (left < right - 1) {
mid = left + (right - left) / 2;
// If num[i-1] < num[i] > num[i+1],then num[i] is peak
if (num[mid] > num[mid - 1] && num[mid] > num[mid + 1]) {
return mid;
}//if
// If num[i-1] < num[i],then num[i+1...n-1] must contains a peak
if (num[mid - 1] < num[mid]) {
left = mid + 1;
}//if
// If num[i-1] > num[i], then num[0...i-1] must contains a peak
else {
right = mid - 1;
}//else
}//while
return num[left] > num[right] ? left : right;
}
};
int main(){
Solution solution;
vector<int> num = {1,2,3,1};
//vector<int> num = {4,3,3,1};
//vector<int> num = {2,3,1,4,2,1};
int result = solution.findPeakElement(num);
// 输出
cout<<result<<endl;
return 0;
}
【分析三】
递归版
【代码三】
/*------------------------------------
* 日期:2015-01-31
* 作者:SJF0115
* 题目: 162.Find Peak Element
* 网址:https://oj.leetcode.com/problems/find-peak-element/
* 结果:AC
* 来源:LeetCode
* 博客:
---------------------------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int findPeakElement(const vector<int> &num) {
return helper(num,0,num.size()-1);
}
private:
int helper(const vector<int> &num,int left,int right){
// only one element
if(left == right){
return left;
}//if
// neighbor
if(left == right - 1){
return num[left] > num[right] ? left : right;
}//if
int mid = left + (right - left) / 2;
// If num[i-1] < num[i] > num[i+1], then num[i] is peak
if(num[mid] > num[mid-1] && num[mid] > num[mid+1]){
return mid;
}//if
// If num[i-1] > num[i], then num[0...i-1] must contains a peak
if(num[mid] < num[mid - 1]){
return helper(num,left,mid - 1);
}//if
// If num[i-1] < num[i],then num[i+1...n-1] must contains a peak
else{
return helper(num,mid + 1,right);
}//else
}
};
int main(){
Solution solution;
//vector<int> num = {1,2,3,1};
//vector<int> num = {4,3,3,1};
vector<int> num = {2,3,1,4,2,1};
int result = solution.findPeakElement(num);
// 输出
cout<<result<<endl;
return 0;
}
分享到:
相关推荐
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
Leetcode 题解.pdf
LeetCode 后端.zip
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
Leetcode101.zip
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
刷leetcode总结.md
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
My Solutions to Leetcode problems. All solutions support C
LeetCode674. 最长连续递增序列674. 最长连续递增序列解题思路:记录每次递增序列的长度,max存储最大长度// 递增序列更新最大长度} else
LeetCode746.使用最小花费爬楼梯746. 使用最小花费爬楼梯解题思路:动态规划当前楼梯最小值=Math.min(前一步最小值,前两步最小值)简化 mi
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
LeetCode888. 公平的糖果棒交换888. 公平的糖果棒交换解题思路:哈希表法第一次遍历 A\B 计算二者的糖果差值 diff,同时以 B糖果值建立哈希