Find Peak Element
来源:互联网 发布:把高中老师给睡了知乎 编辑:程序博客网 时间:2024/05/17 03:30
一、问题描述
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.
二、思路
本题意在找出排序数组中当前元素比下一个元素大的元素,非常简单,遍历即可,而且当存在多个这样的数时,不需要返回其他值。
三、代码
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int peak = 0 , i = 0;
if(nums.size() == 0) return 0;
for(i = 0; i < nums.size() - 1; ++i){
if(nums[i] > nums[i + 1])
break;
}
return i;
}
};
- [LeetCode] Find Peak Element
- Find Peak Element
- Leetcode:Find Peak Element
- Find Peak Element
- Find Peak Element
- Find Peak Element
- LeetCode Find Peak Element
- leetcode: Find Peak Element
- leetcode:Find Peak Element
- Find Peak Element
- Find Peak Element
- Find Peak Element
- LeetCode Find Peak Element
- Find Peak Element --leetcode
- Leetcode Find Peak Element
- [LeetCode] Find Peak Element
- Find Peak Element [Medium]
- Leetcode Find Peak Element
- UESTC 485 Game(康托展开,bfs打表)
- Lightoj 1058
- 数据在底层的传播套接字
- Java 正确使用 volatile
- 利用仿函数实现定制删除器
- Find Peak Element
- Q8--二进制中1的个数
- [JavaWeb] Struts2 入门教程(详细啊!!)
- 网络最高安全机密
- getSharedPreferences(name, mode)两个参数的意思。
- 只需简单的几步配置,就可以免证书真机测试
- 【数据结构】布隆过滤器
- 【数据结构与算法】——排序综述
- js闭包理解