- 浏览: 183502 次
- 性别:
- 来自: 济南
文章分类
最新评论
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)。代码如下:
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:
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Find Peak Element
javascript js_leetcode题解之162-find-peak-element.js
python python_leetcode题解之162_Find_Peak_Element.py
从标题和描述中可以看到,本文主要关注于LeetCode原题的总结,涵盖了162个题目,包括Find Peak Element、Isomorphic Strings、Group Shifted Strings等,这些题目都是Google Onsite面经中的常见题目。 下面将对这些...
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
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
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 ...
- Find Peak Element(寻找峰值元素) - Search in Rotated Sorted Array(在旋转排序数组中搜索) - Search in Rotated Sorted Array II(在旋转排序数组中搜索II) - Find Minimum in Rotated Sorted Array...
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:木材切割问题,给定木材长度,求可以切出的最大...
- **寻找峰值元素(Find Peak Element)**: 给定一个未排序的数组,找到一个“峰值”元素,即其值大于相邻元素。 ##### 位操作(Bit Manipulation) - **缺失数字(Missing Number)**: 给定一个包含[0, n]范围内所有...
- **查找峰值元素(Find Peak Element)** - 在一个一维数组中找到一个峰值元素。 - **旋转排序数组中的搜索(Search in Rotated Sorted Array)** - 在一个旋转过的排序数组中查找一个元素。 以上就是Lintcode算法...
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...
- **查找峰值元素(Find Peak Element)**: 在一个无序数组中找到任意一个局部最大值。 - **在旋转排序数组中搜索/Search in Rotated Sorted Array**: 在一个被旋转过的排序数组中搜索给定数字。 - **在旋转排序数组中...
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-...
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
search_box = driver.find_element_by_id("kw") search_box.send_keys("python") # 模拟按下回车键 search_box.send_keys(Keys.RETURN) # 等待页面加载完成 # 进行其他操作... # 关闭浏览器 driver.quit() ``` ###...