当前位置:网站首页>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
边栏推荐
- 2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
- Authentication code for wireless
- SQL classic exercises (x30)
- Redis和其他数据库的比较
- Rtx3090 installing pytorch3d
- Dqn theoretical basis and code implementation [pytoch + cartpole-v0]
- 未知宽高元素实现水平垂直居中的方法
- MySQL面试题(2022)
- oracle 关闭回收站
- Display zabbix6.0 information using grafana8.5.2
猜你喜欢

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

Several methods of face detection

leetcode 222. 完全二叉树的节点个数(必会)

Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2
![深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]](/img/9f/a0d03b23e66849f12150f9a72f36c5.png)
深入理解机器学习——类别不平衡学习(Imbalanced Learning):样本采样技术-[人工采样技术之SMOTE采样法及Borderline-SMOTE采样法]

Browser cannot open tensorboard

Latest installation tutorial of VMware Tools (rhel8)

通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动

Chengxin University envi_ IDL second week homework: extract aerosol thickness at n points + detailed analysis

04_服务注册Eureka
随机推荐
mysqldump: [Warning] Using a password on the command line interface can be insecure.
洛谷每日三题之第五天
Authentication code for wireless
RuntimeError_ Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
[NoSQL] redis high availability and persistence
Specifications, multi table query basis
Powertor500t reports an error 0x01806803
We should increase revenue and reduce expenditure
374. 猜数字大小(入门 必会)
[template record] string hash to judge palindrome string
Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2
leetcode:50. Pow(x, n)
Dqn theoretical basis and code implementation [pytoch + cartpole-v0]
Net SNMP related commands
Browser cannot open tensorboard
Win10 onedrive failure reinstallation
Polynomial interpolation fitting (I)
MySQL面试题(2022)
ncnn DataReader&Extractor&blob
05_服务调用Ribbon