LeetCode 162. Find Peak Element
来源:互联网 发布:地砖 铺装 软件 编辑:程序博客网 时间:2024/04/29 21:42
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.
题目大意:
找出元素b,假设b元素前面的元素是a,后面的元素是c。则元素b满则b>a切b>c。则返回元素b的下标。如果存在多个,则返回任意一个即可。我们把第一个元素的前面看作负无穷,最后一个元素的后一个也看作负无穷。
题目代码:
class Solution {public: int findPeakElement(vector<int>& nums) { if(nums.size() < 2) return 0; for(int i = 1; i < nums.size(); i++){ if((i!= nums.size()-1 && nums[i] > nums[i-1] && nums[i] > nums[i+1]) || (i==nums.size()-1 && nums[i] > nums[i-1])){ return i; } } return 0; }};
阅读全文
1 0
- [LeetCode]162.Find Peak Element
- LeetCode 162. Find Peak Element
- [leetcode] 162.Find Peak Element
- 【LeetCode】162. Find Peak Element
- Leetcode 162. Find Peak Element
- [LeetCode]162. Find Peak Element
- 162. Find Peak Element LeetCode
- [LeetCode]162. Find Peak Element
- [LeetCode]162. Find Peak Element
- LeetCode *** 162. Find Peak Element
- leetcode.162. Find Peak Element
- LeetCode-162.Find Peak Element
- LeetCode 162. Find Peak Element
- [leetcode] 162. Find Peak Element
- LeetCode 162. Find Peak Element
- LeetCode - 162. Find Peak Element
- [Leetcode]162. Find Peak Element
- LeetCode 162. Find Peak Element
- 明明白白AOP(傻瓜也会心领神会!)
- 深度学习--花式CNN
- maven项目发布到tomcat里lib包没有发布的问题
- bzoj 3401: [Usaco2009 Mar]Look Up 仰望
- c# winform下使用多线程时在线程里如何安全访问窗体控件InvokeRequired
- LeetCode 162. Find Peak Element
- StringUtils之isBlank分析
- HTML和JavaScript单双引号转义
- strlen,strcpy,strcat,strcmp,strstr,memcpy,的模拟实现方式
- Windows上安装Jekyll
- RecycleView加入底部loadmore(四)
- 使用feign作为客户端来消费服务,提供负载均衡和断路器支持
- UML简介
- 联发科编程大赛_第一题最大序列和