1题目

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.

2分析

使用分治策略。

3 源代码

    public int findPeakElement(int[] num) {
int low = 0;
int high = num.length-1; while(low < high)
{
int mid1 = (low+high)/2;
int mid2 = mid1+1;
if(num[mid1] < num[mid2])
low = mid2;
else
high = mid1;
}
return low;
}

(leetcode162)find peak element的更多相关文章

  1. LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element

    二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...

  2. Leetcode162. Find Peak Element寻找峰值

    示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2:   或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...

  3. [Swift]LeetCode162. 寻找峰值 | Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  4. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  5. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  6. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  7. [LintCode] Find Peak Element 求数组的峰值

    There is an integer array which has the following features: The numbers in adjacent positions are di ...

  8. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

  9. 【leetcode】Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

随机推荐

  1. Qt样式表的使用

    Qt中可以灵活的使用层叠样式表(CSS),其语法和CSS很相似.因为HTML CSS的灵活性,所以可以很方便的为QT界面设计自己需要的外观.除了子类化Style类,使用QT样式表(QStyleShee ...

  2. Codeforces 757C. Felicity is Coming!

    C. Felicity is Coming! time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  3. java性能优化之for循环

    完成同样的功能,用不同的代码来实现,性能上可能会有比较大的差别,所以对于一些性能敏感的模块来说,对代码进行一定的优化还是很有必要的.今天就来说一下java代码优化的事情,今天主要聊一下对于for(wh ...

  4. Tag file

    JSP 2.0 引入 Tag file ,tag file 以 tag 或 tagx 为后缀,它们可以包含其他资源文件:一个被其他文件包含的 tag file 应该以 tagf 为后缀. 如同JSP页 ...

  5. Scrum 敏捷开发

    使用敏捷开发一个月的事件,基本的开发模式跟我遇到的这个文章介绍的基本类似,暂时简单Copy到了这里...... http://www.scrumcn.com/agile/scrum-knowledge ...

  6. 如何使用putty远程连接linux

    如何使用putty远程连接linux | 浏览:5001 | 更新:2013-08-24 10:36 1 2 3 4 5 分步阅读 putty是一款超轻量级的运行在windows操作系统上的用于远程连 ...

  7. 2018.12.30 poj3734 Blocks(生成函数)

    传送门 生成函数入门题. 按照题意构造函数: 对于限定必须是出现偶数次的颜色:1+x22!+x44!+...=ex+e−x21+\frac {x^2}{2!}+\frac {x^4}{4!}+...= ...

  8. 2018.12.08 codeforces 939E. Maximize!(二分答案)

    传送门 二分答案好题. 题意简述:要求支持动态在一个数列队尾加入一个新的数(保证数列单增),查询所有子数列的 最大值减平均值 的最大值. 然而网上一堆高人是用三分做的. 我们先考虑当前的答案有可能由什 ...

  9. struts上传文件报argument type mismatch错误

    报错如下图所示: 报错原因:把String 强行转换成FormFile,所以才会抛出argument type mismatch.经查询:表单(html:form)中enctype="mul ...

  10. 安装docker跨主机网络flannel

    一.实验环境 机器 操作系统 安装服务 172.16.4.36 centos7 docker etcd flannel 172.16.4.37 centos7 docker etcd flanne ( ...