Leetcode_162_Find Peak Element
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43415313
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.
思路:
(1)题意为给定一个整型数组,从中找出某一个元素,使得该元素比其左右元素都大,求该元素在数组中的位置。
(2)该题考查的对数组中元素大小的比较。该题还是挺简单的,只不过需要注意的是数组前两个元素和最后两个元素之间的比较。遍历数组一次,即可得到所得元素的下标。只不过这样的解题方法太低端,效率肯定不好。好的方法目前尚未想到,待后续想到再进行补充。这里就不啰嗦,详情见下方代码。
(3)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public int findPeakElement(int[] num) {
int len = num.length;
if (num == null || len < 2) {
return 0;
}
if (len == 2 && num[0] < num[1]) {
return 1;
}
for (int i = 1; i < len; i++) {
if (i + 1 < len && num[i] > num[i - 1] && num[i] > num[i + 1]) {
return i;
}
}
if (num[0] > num[1]) {
return 0;
}
if (num[len - 1] > num[len - 2]) {
return len - 1;
}
return 0;
}
Leetcode_162_Find Peak Element的更多相关文章
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- 【leetcode】Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode Find Peak Element
原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...
- 162. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- mybatis映射器配置细则
前面三篇博客我们已经多次涉及到映射器的使用了,增删查基本上都用过一遍了,但是之前我们只是介绍了基本用法,实际上mybatis中映射器可以配置的地方还是非常多,今天我们就先来看看映射器还有哪些需要配置的 ...
- 安卓7.1 新特性Shortcut
介绍 Shortcut 是谷歌在API25提出来的 类似苹果3D touch 但是没有压力感应.在安卓中完全就是长按. 来看下效果吧: 是不是很赞? 那么请随本文一起学习吧 更新 新建项目 在你项目下 ...
- 让你的代码量减少3倍!使用kotlin开发Android(三) 缩短五倍的Java Bean
回顾一下 哈,没想到你已经坚持不懈看到第三篇了,不错哈~坚持就是胜利. 本文同步自博主的私人博客wing的地方酒馆 在上一篇文章中,我们介绍了扩展函数,这里对上一篇进行一点小小的补充. 还记得text ...
- SQL Join各种内联外联说明
Visual Representation of SQL Joins C.L. Moffatt, 3 Feb 2009 CPOL 4.96 (406 votes) Rate this: This ...
- list标准函数的模拟
;反序 ( ) -> ( ) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons ...
- 初识Java多线程编程
Java 多线程编程 Java给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 多线程是多任务的一种特别 ...
- 优化Javascript数据遍历
问题 M是一个对象的集合,没个对象拥有唯一的字符串类型的Id N是Id的集合. 从M中过滤掉Id不在N中的对象. 假如M有50w个数据,N中可能是0~50w任意的数据. 方案1 使用数组保存Id的集合 ...
- Java编程思想阅读收获
15年8月份买了一本Java编程思想第四版中文版.之所以买中文版是因为我试读了同事的英文版发现自己英语水平还是有限,单词虽然认识,但对很多句子把握不准,这样看书太慢了,要理解英文还要理解技术有些hol ...
- Maven仓库概述
什么是Maven仓库 在Maven世界中,任何一个依赖.插件或项目构建的输出,都可以称为构建.由于Maven引入了坐标机制,任何一个构建都可以由其坐标唯一标识.坐标是一个构建在Maven世界中的逻辑表 ...
- linux 最大文件描述符
Linux对应用程序能打开的的最大文件描述符数量有两个层次的限制:用户级限制和系统级限制. 用户级限制是指目标用户运行的所有进程总共能打开的文件描述符数. 系统级的限制是指所有用户总共能打开的文件描述 ...