Find Peak Element
来源:互联网 发布:淘宝户外用品店那家好 编辑:程序博客网 时间:2024/04/30 02:53
Q:
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.
Note:
Your solution should be in logarithmic complexity.
Solution:
public class Solution { public int findPeakElement(int[] num) { if (num.length == 1) return 0; int i = 0; int j = num.length - 1; int m = (i + j) / 2; while (i <= j) { m = (i + j) / 2; if (m == 0 && num[m] > num[m+1]) return m; if (m == num.length-1 && num[m] > num[m-1]) return m; if (m > 0 && m < num.length-1 && num[m] > num[m-1] && num[m] > num[m+1]) return m; if (m < num.length-1 && num[m] < num[m+1]) i = m + 1; else if (m > 0 && num[m] < num[m-1]) j = m - 1; } return m; }}
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
- 饿汉模式和懒汉模式的多线程访问问题
- Android注册广播两种方式的区别
- IE下margin:0 auto不居中解决方法
- Android中自适应理解
- Android瀑布流照片墙实现,体验不规则排列的美感
- Find Peak Element
- 100以内的质数及100以内质数的和
- 制作映像(dmg)文件详细步骤
- Minimum Window Substring
- 自定义的弹出层。。弹出层不滚动,ie6不兼容
- 服务器如何获取客户端用户的(城市)地址[获得真实IP再获得城市]
- BZOJ 3333: 排队计划 树状数组+线段树
- C 语言程序设计实践 3.11 彩色链子
- Java学习笔记之入门基础