[leetcode-162]Find Peak Element(c)
2015-08-21 10:10
447 查看
问题描述:
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
= -∞.
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.
分析:这道题没什么难度,稍微注意点不要数组越界就好。
代码如下:0ms
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
= -∞.
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.
分析:这道题没什么难度,稍微注意点不要数组越界就好。
代码如下:0ms
[code]int findPeakElement(int* nums, int numsSize) { if(numsSize==1 || nums[0]>nums[1]) return 0; if(nums[numsSize-1]>nums[numsSize-2]) return numsSize-1; for(int i = 1;i<numsSize-1;i++){ int prev = nums[i-1]; int now = nums[i]; int next = nums[i+1]; if(now>prev && now>next) return i; } }
相关文章推荐
- wheelview源码
- 网页分页实现(css+js)
- 一个LINUX狂人的语录(个人认为很精辟)
- 他所有的情商和智商都还停留在原地,可是,我再却已经走出了好远好远。。。
- Python获取本机 IP/MAC(多网卡)
- 最佳实践之 Android代码规范
- struts2注解
- 日经春秋 20150821
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
- Git使用汇总
- 如何在linux中搭建JEECMS系统
- 关于内层DIV设置margin-top不起作用的解决方案
- 程序员如何提高工作效率(cocos2dx+lua+VS2013+BabeLua)
- struts2的result-type详解
- XML解析之----SAX解析(带属性与不带属性)----自定义Handler类
- 天声人語 20150821
- 时间序列模型
- 解析json字符串 及字符串数组
- 解析json字符串 及字符串数组
- 最美应用-从Android研发工程师的角度之[最美时光]