当前位置:网站首页>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))
};
边栏推荐
- 【嵌入式】使用JLINK RTT打印log
- 力扣每日一题(二)
- LeetCode:673. 最长递增子序列的个数
- Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
- 广州推进儿童友好城市建设,将探索学校周边200米设安全区域
- Delay initialization and sealing classes
- TP-LINK enterprise router PPTP configuration
- UML圖記憶技巧
- 同一局域网的手机和电脑相互访问,IIS设置
- Variable length parameter
猜你喜欢
Light of domestic games destroyed by cracking
Computer cleaning, deleted system files
[OC]-<UI入门>--常用控件-UIButton
C語言雙指針——經典題型
优秀的软件测试人员,都具备这些能力
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
Detailed explanation of heap sorting
项目连接数据库遇到的问题及解决
Sublime text using ctrl+b to run another program without closing other runs
The harm of game unpacking and the importance of resource encryption
随机推荐
UML图记忆技巧
MySQL uninstallation and installation methods
项目连接数据库遇到的问题及解决
Deep analysis of C language pointer
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
Shift Operators
Alibaba cloud server mining virus solution (practiced)
LeetCode:221. Largest Square
[sword finger offer] serialized binary tree
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
有效提高软件产品质量,就找第三方软件测评机构
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
POI add write excel file
LeetCode:41. Missing first positive number
The mysqlbinlog command uses
Nacos 的安装与服务的注册
LeetCode:387. The first unique character in the string
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
LeetCode:221. 最大正方形
Swagger setting field required is mandatory