LeetCode 162.Find Peak Element(M)(P)
题目:
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1]
, find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞
.
For example, in array [1, 2, 3, 1]
, 3 is a peak element and your function should return the index number 2.
Note:
Your solution should be in logarithmic complexity.
思路:
1.查找,时间复杂度O(logn)使用二分法。
2.在如下图所示情况下,有极大值:
3.长度为1时,由于nums[-1]和nums[n]为负无穷,故0即为极大值下标。
4.根据nums[mid]不同情况分析:
(1)nums[mid]比两边数大,mid即为极大值下标。
(2)nums[mid]比左边大,比右边小,极大值可能在mid右侧,故start= mid。
(3)nums[mid]比右边大,比左边小,极大值可能在mid左侧,故end=mid。
(4)nums[mid]比两边都小,根据nums[end]与nums[end-1]判断。
代码:
public class Solution {
public int findPeakElement(int[] nums) {
int start = 0, end = nums.length-1,mid = 0;
if(nums.length == 1){
return 0;
}
while(start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]){
return mid;
}else if(nums[mid] < nums[mid+1] && nums[mid] > nums[mid-1]){
start = mid;
}else if(nums[mid] > nums[mid+1] && nums[mid] < nums[mid-1]){
end = mid;
}else if(nums[end-1] > nums[end]){
start = mid;
}else{
end = mid;
}
}
if(nums[start] > nums[end]){
return start;
}
return end;
}
}
LeetCode 162.Find Peak Element(M)(P)的更多相关文章
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- ✡ leetcode 162. Find Peak Element --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- leetcode 162 Find Peak Element(二分法)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- 吴裕雄--天生自然C语言开发:共同体
union [union tag] { member definition; member definition; ... member definition; } [one or more unio ...
- 查看linux系统安装的服务
如何查看linux系统安装了哪些服务呢,因不同版本的操作系统可能使用的命令不一样或者有些命令在某些操作系统不可用,现列举一些常用查看命令(基于我的linux版本). 我的操作系统版本如下: 1.ser ...
- Mysql 和 Java对比异同
1.求两个时间的差(天数) mysql : to_days 距离公元0年的天数 select TO_DAYS('2017-10-18 00:00:00'),TO_DAYS(NOW()), (TO_DA ...
- Spring定义Bean的两种方式:和@Bean
前言: Spring中最重要的概念IOC和AOP,实际围绕的就是Bean的生成与使用. 什么叫做Bean呢?我们可以理解成对象,每一个你想交给Spring去托管的对象都可以称之为Bean. 今天 ...
- [Machine Learning] Andrew Ng on Coursera (Week 1)
Week 1 的内容主要有: 机器学习的定义 监督式学习和无监督式学习 线性回归和成本函数 梯度下降算法 线性代数回归 主要是了解一下机器学习的基本概念,重点是学习线性回归模型,以及对应的成本函数和梯 ...
- python学习笔记(6)数据类型-集合
集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典. 创建格 ...
- python易错知识集合
本篇用于记录在写leetcode时遇到的python易错知识. 2019.8.29 1.Python range() 函数用法: range(start, stop[, step]) start: 计 ...
- file,path,uri互相转换
uri 转 file :File file = new File(new URI(uri.toString())); uri 转 path: Path path = Files.get(uri); f ...
- SpringBoot连接Oracle报错,找不到驱动类,application.properties文件中驱动类路径为红色
pom.xml文件: <!-- oracle odbc --> <dependency> <groupId>com.oracle</groupId> & ...
- nodejs 中国汉字模糊查询简单(很low)实现
https://github.com/cclient/hanzimohusearch 部分代码