当前位置:网站首页>LeetCode:162. Looking for peak
LeetCode:162. Looking for peak
2022-07-06 08:51:00 【Bertil】
The peak element refers to the element whose value is strictly greater than the left and right adjacent values .
Give you an array of integers nums, Find the peak element and return its index . The array may contain multiple peaks , under these circumstances , return Any peak Just where you are .
You can assume nums[-1] = nums[n] = -∞ .
You must achieve a time complexity of O(log n) Algorithm to solve this problem .
Example 1:
Input :nums = [1,2,3,1]
Output :2
explain :3 Is the peak element , Your function should return its index 2.
Example 2:
Input :nums = [1,2,1,3,5,6,4]
Output :1 or 5
explain : Your function can return the index 1, Its peak element is 2;
Or return index 5, Its peak element is 6.
Tips :
- 1 <= nums.length <= 1000
- -2^31 <= nums[i] <= 2^31 - 1
- For all that works i There are nums[i] != nums[i + 1]
Their thinking
1. First, find the largest element , Then return to its index
2. explain : Why is the maximum the answer ?
- Because the title has assumed nums[-1] = nums[n] = -∞, So the peak will not appear in the first and last elements
- The left and right adjacent elements of the maximum value in the array must be less than the maximum value
Code
/** * @param {number[]} nums * @return {number} */
var findPeakElement = function(nums) {
return nums.indexOf(Math.max(...nums))
};
边栏推荐
猜你喜欢
[sword finger offer] serialized binary tree
View computer devices in LAN
[OC]-<UI入门>--常用控件的学习
多元聚类分析
Cesium draw points, lines, and faces
如何正确截取字符串(例:应用报错信息截取入库操作)
Mobile phones and computers on the same LAN access each other, IIS settings
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
[OC-Foundation框架]---【集合数组】
随机推荐
UML diagram memory skills
Double pointeur en langage C - - modèle classique
【ROS】usb_ Cam camera calibration
After reading the programmer's story, I can't help covering my chest...
LeetCode:剑指 Offer 03. 数组中重复的数字
Charging interface docking tutorial of enterprise and micro service provider platform
Generator parameters incoming parameters
LeetCode:39. Combined sum
Philosophical enlightenment from single point to distributed
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
Current situation and trend of character animation
LeetCode:劍指 Offer 42. 連續子數組的最大和
项目连接数据库遇到的问题及解决
【嵌入式】使用JLINK RTT打印log
Visual implementation and inspection of visdom
Roguelike game into crack the hardest hit areas, how to break the bureau?
LeetCode:214. 最短回文串
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
Tcp/ip protocol
Navicat premium create MySQL create stored procedure