本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

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

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

  2. LeetCode 162 Find Peak Element

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

  3. Find Peak Element

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

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

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

  5. lintcode 75 Find Peak Element

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

  6. 【leetcode】Find Peak Element

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

  7. 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] ≠ ...

  8. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  9. 162. Find Peak Element

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

随机推荐

  1. [extjs5学习笔记]第三十八节 sencha CMD 6.0.0.220版本安装

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/46740381 简介 sencha cmd 6安装过程不需要UAC控制了对于widnow ...

  2. Redis Cluster架构优化

    Redis Cluster架构优化 在<全面剖析Redis Cluster原理和应用>中,我们已经详细剖析了现阶段Redis Cluster的缺点: 无中心化架构 Gossip消息的开销 ...

  3. RxJava(七) 使用debounce操作符 优化app搜索功能

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51555203 本文出自:[余志强的博客] 一.抛出问题 现在几乎所有 ...

  4. Android文件(File)操作

    Android 使用与其他平台上基于磁盘的文件系统类似的文件系统. 本文讲述如何使用 Android 文件系统通过 File API 读取和写入文件. File 对象适合按照从开始到结束的顺序不跳过地 ...

  5. [Mysql]mysql windows下配置文件

    环境是win7 mysql5.6版本 测试下配置文件是否可用(之前没用过windows下的msyql配置) 修改配置前查询下: mysql> show variables like '%max_ ...

  6. maven项目管理

    systemPath方式 有些不通用的包,maven仓库没有,只能通过本地包依赖,就像下面方式: 在需要依赖的项目建lib文件夹,如下: 然后在pom.xml项目管理文件里面加入本地依赖,如下 这种情 ...

  7. 剑指offer面试题6 重建二叉树(c)

  8. 05_MyBatis基于注解的开发

     要想开发基于注解的MyBatis应用.需要先写一个带有注解的接口. PersonDao.java的写法如下: package com.rl.dao; import java.util.List; ...

  9. 如何找到java对应的c/c++源码

    很多时候java经常被c鄙视,因为c说我是你的基础,但java竟然有如此强的生命力就必然有其存在的价值.本文不探讨各种开发语言的优劣,仅仅介绍如何找到java对应c/c++实现的源码.当我们追究一个j ...

  10. 安卓java.lang.IllegalArgumentException: The observer is null.解决方案

    刚刚在调试自己的APP项目的时候报错java.lang.IllegalArgumentException: The observer is null.,而之前是可以运行通过,所以百思不得其解,后来在网 ...