`

Find Peak Element

阅读更多
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.

按照题目的要求,从一个数组中找到一个数比它的邻居都大。数组有几个特征,没相邻的两个元素不同,并且题目中假设num[-1] = num[n] = -∞。这样我们可以确定的是除非数组为空,否则数组中肯定会存在一个peak element。我们可以用二分法,从中间元素开始比较,如果nums[m] > nums[m + 1]说明m + 1左边肯定存在peak元素,我们就从左部分找;如果nums[m] < nums[m + 1]说明m右边肯定存在peak元素,我们从右半部分查找。这样时间复杂度为O(nlogn)。代码如下:
public class Solution {
    public int findPeakElement(int[] nums) {
        if(nums == null || nums.length == 0) return -1;
        if(nums.length == 1) return 0;
        int l = 0;
        int r = nums.length - 1;
        while(l < r) {
            int m = l + (r - l) / 2;
            if(nums[m] > nums[m + 1]) {
                r = m;
            } else {
                l = m + 1;
            }
        }
        return l;
    }
} :twisted: 
0
1
分享到:
评论

相关推荐

    java-leetcode题解之Find Peak Element.java

    java java_leetcode题解之Find Peak Element

    js-leetcode题解之162-find-peak-element.js

    javascript js_leetcode题解之162-find-peak-element.js

    python-leetcode题解之162-Find-Peak-Element.py

    python python_leetcode题解之162_Find_Peak_Element.py

    2018年最新Google Onsite面经总结1

    从标题和描述中可以看到,本文主要关注于LeetCode原题的总结,涵盖了162个题目,包括Find Peak Element、Isomorphic Strings、Group Shifted Strings等,这些题目都是Google Onsite面经中的常见题目。 下面将对这些...

    lrucacheleetcode-Coding-Interview:编程面试

    lru cache leetcode Coding-Interview ...Peak Element First Bad Version Valid Perfect Square 二叉树查找/BST查找 Closest Binary Search Tree Value 二叉树查找/二叉树第K个 Kth Smallest Element In A

    LeetCode C++全解

    1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array iii....iv....v....vi....vii. Find Minimum in Rotated Sorted Array viii....ix....x....xi....xii....xiii....xiv. Find Peak Element

    leetcode分类-interview:面试基础算法

    peak element 34: find first and last position of element in sorted array 300: longest increasing subsequence hard 154: find minimum in rotated sorted array ii 315: count of smaller numbers after self ...

    算法刷题笔记leetcode/lintcode

    - Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array(在旋转排序数组中搜索) - Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array...

    gasstationleetcode-Leetcode:力码

    element(162).swift Leetcode\find重复的数字(287).swift Leetcode\friend circles(547).swift Leetcode\gas station(134)。 swift Leetcode\group anagrams(49).swift Leetcode\group 给定他们所属的组大小的人...

    算法学习笔记

    - Find Peak Element:在一个整数数组中找到一个峰值元素。 - Median of Two Sorted Arrays:两个排序数组的中位数。 - Sqrt(x):计算并返回x的平方根。 - Wood Cut:木材切割问题,给定木材长度,求可以切出的最大...

    LeetCode练习答案

    - **寻找峰值元素(Find Peak Element)**: 给定一个未排序的数组,找到一个“峰值”元素,即其值大于相邻元素。 ##### 位操作(Bit Manipulation) - **缺失数字(Missing Number)**: 给定一个包含[0, n]范围内所有...

    lintcode算法分析和解答

    - **查找峰值元素(Find Peak Element)** - 在一个一维数组中找到一个峰值元素。 - **旋转排序数组中的搜索(Search in Rotated Sorted Array)** - 在一个旋转过的排序数组中查找一个元素。 以上就是Lintcode算法...

    javalruleetcode-leetcode-python:leetcode问题的Python解决方案

    Find Peak Element.Binary search,需要比较nums[mid]和nums[mid+1]. 4.12 212题. Word Search II. 用trie tree存word list,然后dfs. 4.11 788题. Rotated Digits.转换成字符串,时间复杂度O(nlogn).还可以用dp,dp...

    算法-leetcode-剑指offer上的题很多

    - **查找峰值元素(Find Peak Element)**: 在一个无序数组中找到任意一个局部最大值。 - **在旋转排序数组中搜索/Search in Rotated Sorted Array**: 在一个被旋转过的排序数组中搜索给定数字。 - **在旋转排序数组中...

    lrucacheleetcode-leetcode:个人刷leetcode遇到的一些题汇总(golang)

    find-peak-element longest-common-subsequence longest-consecutive-sequence max-area-of-island next-greater-element-ii serialize-and-deserialize-binary-tree subarray-sum-equals-k binary-tree-preorder-...

    Simulation of Brillouin gain properties in a double-clad As2Se3 chalcogenide photonic crystal fiber

    We also find that a larger size of inner cladding air holes will lead to a more pronounced second peak in the BGS. On the other hand, the size of the outer cladding has nearly no effect on the

    selenium python自动化测试环境搭建.docx

    search_box = driver.find_element_by_id("kw") search_box.send_keys("python") # 模拟按下回车键 search_box.send_keys(Keys.RETURN) # 等待页面加载完成 # 进行其他操作... # 关闭浏览器 driver.quit() ``` ###...

Global site tag (gtag.js) - Google Analytics