Find Peak Element
2015-08-13 14:29
190 查看
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.
找到一个波峰元素。特殊情况是最后一个大于倒数第二个,也算波峰。
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.
找到一个波峰元素。特殊情况是最后一个大于倒数第二个,也算波峰。
public int findPeakElement(int[] nums) { if(nums==null||nums.length<2) return 0; if(nums[nums.length-1]>nums[nums.length-2]) return nums.length-1; for(int i=1;i<nums.length-1;i++){ if(nums[i]>nums[i-1]&&nums[i]>nums[i+1]) return i; } return 0; }
相关文章推荐
- struts2 中chain、redirect、redirectaction的区别
- pymongo 使用
- kaggle bike sharing program R code tranlated from python code ranked 9th
- [iOS]字符串转字典
- java基础知识实例
- mysql重置密码
- POJ 3020 Antenna Placement(二分匹配最小路径覆盖)
- kaggle bike sharing program R code tranlated from python code ranked 9th
- Linux中环境变量文件及配置
- CentOS下在大文件中查找某个字符串
- 练钢琴想起的关于耐心信心和勇气
- 大型web系统中缓存的使用
- servlet取得初始化参数值,参数值为资源文件
- 设计模式:24 加薪非要老总批?_职责链模式
- MVP和MVC模式解读与实战运用(一)
- block块页面传值
- 【转】android 5.0 64bit系统加载库文件失败问题浅析
- hadoop本地库无法加载
- copy
- 博弈论--两兄弟分钱