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.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

给一个数组,寻找里面的峰值元素。峰值是比它两边的元素都大。

提示了用log的时间复杂度,所以考虑用二分法Binary Search。

规律一:如果nums[i] > nums[i+1],则在i之前一定存在峰值元素
规律二:如果nums[i] < nums[i+1],则在i+1之后一定存在峰值元素

参考:Orange橘子洲头

Java:

public class Solution {
public int findPeakElement(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if(nums[mid] < nums[mid + 1]) left = mid + 1;
else right = mid;
}
return left;
}
}   

Python:

class Solution(object):
def findPeakElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(nums) - 1 while left < right:
mid = left + (right - left) / 2
if nums[mid] > nums[mid + 1]:
right = mid
else:
left = mid + 1 return left

C++:

class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size() - 1; while (left < right) {
const auto mid = left + (right - left) / 2;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
} return left;
}
};

    

All LeetCode Questions List 题目汇总

[LeetCode] 162. Find Peak Element 查找峰值元素的更多相关文章

  1. LeetCode 162. 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 && lintcode 75. Find Peak Element

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

  3. ✡ leetcode 162. Find Peak Element --------- java

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

  4. LeetCode 162 Find Peak Element

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

  5. 162 Find Peak Element 寻找峰值

    峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...

  6. LeetCode 162.Find Peak Element(M)(P)

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  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 162 Find Peak Element(二分法)

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

  9. LeetCode Problem 169: Majority Element查找多数元素

    描述:Given an array of size n, find the majority element. The majority element is the element that app ...

随机推荐

  1. Calendar.getInstance()获取指定时间点(定时)

    public class Test1 { public static void main(String[]args){ System.out.println("时间为:\n"+ge ...

  2. P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]

    题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...

  3. windows——快速得到某一目录下所有文件的名称

    前言 其实用的是windows上dir命令,能快速得到某一目录下的所有文件名称,天天那么忙都没时间写博客(┬_┬) 步骤 打开cmd并cd到某目录下 C:\Users\Administrator.KI ...

  4. Touch事件 移动端touch触摸事件

    <!-- HTML5 --> <!DOCTYPE html> <html> <head> <title>TouchEvent测试</t ...

  5. Java 锁(学习笔记)

    关于Java 锁的知识整理与回顾(个人笔记): 锁有哪些,分别用来干嘛? Java实现锁有两种方式,synchronized关键字和Lock (1)Lock(可判断锁状态) Lock是基于JDK层面实 ...

  6. Tips on Acoustic Signal Processing

    1.声音的三个主要的主观属性(即音量.音调.音色).音色(Timbre)是指不同的声音的频率表现在波形方面总是有与众不同的特性,音色的不同取决于不同的泛音.频率的高低决定声音的音调,振幅的大小决定声音 ...

  7. Java web开发——文件的上传和下载

    一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件和文件夹进行上传:支持PC端全平台操作系统,Windows,Linux,Mac 支持文件和文件夹的批量下载,断点续传.刷新页面后继续传输. ...

  8. Flutter 简介(事件、路由、异步请求)

    1. 前言 Flutter是一个由谷歌开发的开源移动应用软件开发工具包,用于为Android和iOS开发应用,同时也将是Google Fuchsia下开发应用的主要工具.其官方编程语言为Dart. 同 ...

  9. DACL原理.控制文件的访问权限(文件,注册表.目录.等任何带有安全属性的对象.)

    目录 一丶简介 1.DACL是什么. 2.如何创建一个自己控制的文件. 3.SDDL是个什么鬼. 二丶 编写SDDL 控制的文件 一丶简介 1.DACL是什么. DACL称为自主访问的控制列表.是应用 ...

  10. linux定时执行shell脚本

    写一个shell脚本,定时执行简单示例 很多时候我们有希望服务器定时去运行一个脚本来触发一个操作,比如说定时去备份服务器数据.数据库数据等 不适合人工经常做的一些操作这里简单说下 Shell俗称壳,类 ...