当前位置:网站首页>leetcode162. Looking for peak
leetcode162. Looking for peak
2022-07-19 03:29:00 【only-qi】
Problem description :
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 :
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
-231 <= nums[i] <= 231 - 1
For all that works i There are nums[i] != nums[i + 1]
Code up , Take it to run :
package com.onlyqi.test03.erfen;
/**
* @author onlyqi
* @date 2022/7/12 10:21
* @description
*/
public class Test04 {
public static void main(String[] args) {
int[]nums = {1,2,1,3,5,6,4};
System.out.println(findPeakElement(nums));
}
public static int findPeakElement(int[] nums) {
int start = 0, end = nums.length - 1;
while (start < end ) {
int mid = (start + end ) / 2;
if (nums[mid] > nums[mid + 1])
end = mid;
else
start = mid + 1;
}
return start;
}
/**
* Easy to think of Solutions
* @param nums
* @return
*/
public static int findPeakElement1(int[] nums) {
for (int i = 1; i < nums.length; i++) {
if(nums[i]>nums[i-1]&&nums[i]>nums[i+1]){
return i;
}
}
return 0;
}
}
Running results :

I want to brush it 300 Algorithm questions , The first 105 Avenue
边栏推荐
- Yolov5 opencv DNN reasoning
- 【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
- Cmake common commands
- ncnn paramdict&modelbin
- My most productive easypypi once again has been updated! V1.4.0 release
- Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]
- Leetcode: 0-1 knapsack problem in dynamic programming [come and set the template directly]
- Polynomial interpolation fitting (III)
- JDBC connection to MySQL database
- 2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
猜你喜欢

After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle

基于MFC如何实现单个文档的文件读写

Transaction and storage engine in MySQL database

Yolov6 learning first chapter

ubuntu清除cuda缓存

MySQL面试题(2022)
![[MCU simulation] (XX) org - set start address](/img/9e/4e44dd779b0de28a190d86fbb1c2c0.png)
[MCU simulation] (XX) org - set start address
![mysqldump: [Warning] Using a password on the command line interface can be insecure.](/img/91/8b0d35f85bc0f46daac4e1e9bc9e34.png)
mysqldump: [Warning] Using a password on the command line interface can be insecure.

Affine transformation implementation
![Theoretical basis of double Q-learning and its code implementation [pendulum-v0]](/img/f4/d281bf9e0534aed1d802d32ac35782.png)
Theoretical basis of double Q-learning and its code implementation [pendulum-v0]
随机推荐
ncnn DataReader&Extractor&blob
Win10 onedrive failure reinstallation
[MCU simulation] (XIX) introduction to assembly, assembly instructions, pseudo instructions
Can't access this website can't find DNS address DNS_ PROBE_ What about started?
ES6 learning notes - brother Ma at station B
XX市高中网络拓扑整体规划配置
Chengxin University envi_ IDL second week homework: extract aerosol thickness at n points + detailed analysis
Pytorch best practices and code templates
Vscode+ros2 environment configuration
The installation software prompts that the program input point adddlldirectory cannot be located in the dynamic link library kernel32 DLL (download address at the end of the text)
In depth understanding of machine learning - unbalanced learning: sample sampling technology - [smote sampling method and borderline smote sampling method of manual sampling technology]
ES6学习笔记——B站小马哥
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之ADASYN采样法]
洛谷每日三题之第三天(第四天补做)
Latest installation tutorial of VMware Tools (rhel8)
367. 有效的完全平方数(入门必会)
Go语言中的Iota关键字怎么使用
Labelme starts normally, but cannot be opened
Pure virtual function
Note: light source selection and Application