当前位置:网站首页>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
边栏推荐
- MySQL replication table
- By voting for the destruction of STI by Dao, seektiger is truly community driven
- Several methods of face detection
- IEEE754 standard floating point format
- Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
- Vscode+ros2 environment configuration
- Win10 network connection shows no network but Internet access
- Go语言中的Iota关键字怎么使用
- MySQL log management and full backup incremental backup and recovery
- [NoSQL] redis configuration and optimization of NoSQL (simple operation)
猜你喜欢

神经网络学习笔记2.2 ——用Matlab写一个简单的卷积神将网络图像分类器

05 central processing unit

Visual analysis of ncnn param file and bin model

Authentication code for wireless

Using gatekeeper to restrict kubernetes to create specific types of resources

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

About 1000base-t1 1000Base-TX and 100base-t1

Yolov5 opencv DNN reasoning

Face key point detection

Automatic assembly & set injection
随机推荐
Pure virtual function
[MCU simulation] (XIV) instruction system bit operation instructions - bit data transmission instructions MOV, bit variable modification instructions
[MCU simulation] (XX) org - set start address
Vs code problem: launch:program '... \ vscode\launch. exe‘ dose not exist
洛谷每日三题之第三天(第四天补做)
[MySQL] MHA high availability
MySQL面试题(2022)
Yolov5 opencv DNN reasoning
Transaction and storage engine in MySQL database
Solve the error of 0x00000709 when win10 connects to the shared printer
A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
04_ Service registration Eureka
My most productive easypypi once again has been updated! V1.4.0 release
Latest installation tutorial of VMware Tools (rhel8)
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
Unicast、Multicast、Broadcast
leetcode:50. Pow(x, n)
leetcode162. 寻找峰值
Multi table query - case exercise
数据源对象管理(第三方对象资源) & 加载properties文件