LeetCode 162. 寻找峰值(Find Peak Element) 29
162. 寻找峰值
162. Find Peak Element
题目描述
峰值元素是指其值大于左右相邻值的元素。
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。
数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞。
每日一算法2019/6/1Day 29LeetCode162. Find Peak Element
示例 1:
输出: 2
解释: 3 是峰值元素,你的函数应该返回其索引 2。
示例 2:
输出: 1 或 5
解释: 你的函数可以返回索引 1,其峰值元素为 2;或者返回索引 5,其峰值元素为 6。
说明:
你的解法应该是 O(logN) 时间复杂度的。
Java 实现
class Solution {
public int findPeakElement(int[] nums) {
if (nums == null || nums.length == 0) {
return -1;
}
int low = 0, high = nums.length - 1;
while (low < high) {
int mid1 = low + (high - low) / 2;
int mid2 = mid1 + 1;
if (nums[mid1] < nums[mid2]) {
low = mid2;
} else {
high = mid1;
}
}
return low;
}
}
相似题目
参考资料
- https://leetcode.com/problems/find-peak-element/
- https://leetcode-cn.com/problems/find-peak-element/
LeetCode 162. 寻找峰值(Find Peak Element) 29的更多相关文章
- Java实现 LeetCode 162 寻找峰值
162. 寻找峰值 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下,返 ...
- [Swift]LeetCode162. 寻找峰值 | 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)
Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...
- 领扣(LeetCode)寻找峰值 个人题解
峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位 ...
- LeetCode162.寻找峰值
162.寻找峰值 描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下 ...
- [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 (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode OJ:Find Peak Element(寻找峰值元素)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- Go语言在国产CPU平台上应用前景的探索与思考
http://www.chinaaet.com/article/3000087559 0 引言 CPU是电子产品的核心,代表着信息产业的发展水平.CPU发展至今已经有四十多年的历史了,实际就是Inte ...
- SpringBoot源码分析-编译环境与新建测试模块
建议 分析源码建议不要使用Idea或者Eclipse等IDE工具的反编译功能或者导入源码包的方式看源码,那样不能给框架的源码做注释,所以分析源码之前都得先下载源码并构建,然后在项目中新建一个Modul ...
- Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)
链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...
- Logistic Regression Algorithm
逻辑回归算法LR. 简介 逻辑回归是机器学习从统计学领域借鉴的另一种技术.它是二进制分类问题的首选方法(有两个类值的问题). Logistic回归就像线性回归,目标是找到权重每个输入变量的系数值. ...
- LeetCode 838. Push Dominoes
原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...
- sphinx doc 文档生成脚手架工具
sphinx 在python 语言开发中,是一个使用的比较多文档生成脚手架工具,我们帮助我们生成 专业的帮助文档,同时也有远端的免费saas 托管服务,方便分发 安装 sphinx 的安装好多方便,m ...
- chsh
修改shell进程
- 洛谷 P2918 [USACO08NOV]买干草Buying Hay 题解
P2918 [USACO08NOV]买干草Buying Hay 题目描述 Farmer John is running out of supplies and needs to purchase H ...
- 浅谈SPFA判负环
目录 SPFA判负环 [前言] [不可代替性] [具体实现] SPFA的过程 判负环 [核心代码] [例题] SPFA判负环 有不足的地方请指出 本蒟蒻一定会修改吼 [前言] 最短路的求法中最广为人知 ...
- Alibaba Nacos:搭建Nacos平台
1.下载安装包 https://github.com/alibaba/nacos/releases 往下翻,找到压缩包下载. 2.解压 tar -xvf nacos-server-$version.t ...