当前位置:网站首页>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))
};
边栏推荐
- Deep analysis of C language data storage in memory
- Mobile phones and computers on the same LAN access each other, IIS settings
- Mongodb installation and basic operation
- 【ROS】usb_ Cam camera calibration
- marathon-envs项目环境配置(强化学习模仿参考动作)
- Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
- After reading the programmer's story, I can't help covering my chest...
- 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
- Computer graduation design PHP Zhiduo online learning platform
猜你喜欢
Promise 在uniapp的简单使用
[OC]-<UI入门>--常用控件的学习
项目连接数据库遇到的问题及解决
Roguelike game into crack the hardest hit areas, how to break the bureau?
vb. Net changes with the window, scales the size of the control and maintains its relative position
Deep analysis of C language data storage in memory
Deep anatomy of C language -- C language keywords
LeetCode:124. 二叉树中的最大路径和
The harm of game unpacking and the importance of resource encryption
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
使用latex导出IEEE文献格式
如何正确截取字符串(例:应用报错信息截取入库操作)
Restful API design specification
Niuke winter vacation training 6 maze 2
Visual implementation and inspection of visdom
704 binary search
Introduction to the differences between compiler options of GCC dynamic library FPIC and FPIC
LeetCode:162. 寻找峰值
Sublime text using ctrl+b to run another program without closing other runs
LeetCode:劍指 Offer 42. 連續子數組的最大和
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
ROS compilation calls the third-party dynamic library (xxx.so)
随手记01
The mysqlbinlog command uses
多元聚类分析
LeetCode:214. Shortest palindrome string
Light of domestic games destroyed by cracking
Export IEEE document format using latex
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
How to conduct interface test? What are the precautions? Nanny level interpretation