Find Peak Element
来源:互联网 发布:淘宝包邮软件 编辑:程序博客网 时间:2024/05/17 05:07
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.
click to show spoilers.
解析:遍历每个元素,找左右大大小关系
代码:
class Solution {public: int findPeakElement(vector<int>& nums) { int ans=0; for (int i=0; i<nums.size(); i++) { int left=0; int right=0; if ((i-1)<0) { left=INT_MIN; } else { left=nums[i-1]; } if ((i+1)>=nums.size()) { right=INT_MIN; } else { right=nums[i+1]; } if ((nums[i]>right)&&(nums[i]>left)) { return i; } } return 0; }};
0 0
- [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
- 服务器性能查看常用命令
- arm学习笔记2
- 关于mysql与eclipse进行连接的一些问题
- Java线程间通信
- 编程最佳字体Inconsolata和Consolas
- Find Peak Element
- Android binder 自学
- IntelliJ Idea 2017 免费激活方法
- 当for循环,fork(),printf()相遇时的思考
- Windows系统下无线链接android
- ES5中新增的Array方法详细说明
- “System.AccessViolationException”类型的未经处理的异常在 未知模块。
- Jersey框架入门学习:Jersey图片服务器搭建总结
- java中计算包含汉字字符串的长度