Find Peak Element
来源:互联网 发布:开淘宝网店攻略 编辑:程序博客网 时间:2024/06/03 17:44
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
#include<stdio.h>int findPeakElement(const int num[],int n) { int i; if(n==1) return 0; for(i=1;i<n-1;i++){ if(num[i]>num[i+1] && num[i]>num[i-1]) return i; } if(num[1]<num[0]) return 0; return n-1;}void main(){ int A[]={1,2,3,1}; int n=4; printf("%d\n", findPeakElement(A,n));}
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
- jQuery noConflict() 方法
- redhat升级mysql出现服务启动找不到sock的问题
- mac升级python3.4
- 关于YUV
- ARC指南1 - strong和weak指针
- Find Peak Element
- Oracle——RMAN跨平台支持
- jquery 回车事件
- 建国钞带领纪念钞强势崛起
- 关于谷歌GJSON的使用方法【java和json如何互转】
- java最简单的篇程
- MVC .NET4 IE10下FormsAuthentication.SetAuthCookie无效的问题
- 计算机网络面试题
- 对于Exception的内容输出