本文是在学习中的总结,欢迎转载但请注明出处: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. Unity3D各平台Application.xxxPath的路径

    前几天我们游戏在一个同事的Android手机上启动时无法正常进入,经查发现Application.temporaryCachePath和Application.persistentDataPath返回 ...

  2. 粗浅看Struts2和Hibernate框架

    ----------------------------------------------------------------------------------------------[版权申明: ...

  3. Rails 4.0 bundle exec rspec spec/requests/xxx 测试失败的解决

    rails项目没有使用默认的单元测试包,而是使用了rspec-rails来测试. 按照文档说明首先生成对应的测试文件: rails generate integration_test xxx invo ...

  4. git > 2.3 实现同步盘的功能

    话不多说,简单粗暴 http://stackoverflow.com/questions/35643201/how-to-set-up-a-sychronous-directory-in-remote ...

  5. Dubbo框架应用之(四)--Dubbo基于Zookeeper实现分布式实例

    上三篇文章主要是解决了概念性的补充和学习,充分结合实战来深入理解 入门实例解析 第一:provider-提供服务和相应的接口 创建DemoService接口 package com.unj.dubbo ...

  6. vbs注册表增删改

    vbs注册表增删改非常简单.过去竟然能忍受那么多次手动在注册表编辑器操作...应该认真反思自己的懒惰了. Dim op Set op=WScript.CreateObject("WScrip ...

  7. 没事不要在for循环期间增减迭代序列的成员

    >>> arr=[4, 4, 9, 7, 7] >>> for i,a in enumerate(arr): arr.pop(i) print(i,a) 4 0 4 ...

  8. Python 3 智能发音

    真是十分神奇.. import win32com.client import time s = win32com.client.Dispatch("SAPI.SpVoice") s ...

  9. AP模块NOTE修改API

    --创建 AP_NOTES_PUB.Create_Note ( p_api_version IN NUMBER , p_init_msg_list IN VARCHAR2 := FND_API.G_F ...

  10. 快速索引 (对View的自定义)

    快速索引 (对View的自定义) 快速索引应用场景: 微信好友列表, 联系人通讯录, 应用管理, 文件管理等. 快速索引7步曲: *1. A-Z索引的绘制. * 2. 处理Touch事件. * 3. ...