【刷题-LeetCode】162 Find Peak Element
- Find Peak Element
A peak element is an element that is greater than its neighbors.
Given an input array nums, where nums[i] ≠ nums[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 nums[-1] = nums[n] = -∞.
Example 1:
Input: nums = [1,2,3,1]
Output: 2
Explanation: 3 is a peak element and your function should return the index number 2.
Example 2:
Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where the peak element is 2,
or index number 5 where the peak element is 6.
Follow up: Your solution should be in logarithmic complexity.
解法1 线性扫描
解法2 二分查找
- nums[mid] > nums[mid+1]:nums[mid+1]一定不是峰值,[l, mid]搜索
- nums[mid] <= nums[mid+1]:nums[mid+1]可能是峰值,在[mid+1, r]搜索
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int l = 0, r = nums.size()-1;
while(l < r){
int mid = (l+r)/2;
if(nums[mid] > nums[mid+1])r=mid;
else l = mid+1;
}
return l;
}
};
【刷题-LeetCode】162 Find Peak Element的更多相关文章
- 【leetcode刷题笔记】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 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(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
随机推荐
- LuoguP6861 [RC-03] 难题 题解
Update \(\texttt{2020.10.21}\) 删除了不需要的 \(n=1\) 的特判,并在符号与字母之间添加了空格. Content 给定一个数 \(n\),试找到一对数 \(a,b( ...
- [C# Expression] 之基础概念
00 | 什么是表达式树 表达式树以树形数据结构表示代码,其中每一个节点都是一种表达式,比如方法调用和 x < y 这样的二元运算等.可以对表达式树中的代码进行编辑和运算. 这样能够动态修改可执 ...
- 逆波兰(非与或)表达式原理及C++代码实现
p.p1 { margin: 0; font: 11px Menlo; color: rgba(209, 47, 27, 1); background-color: rgba(255, 255, 25 ...
- 使用unc0ver(5.3.1)执行jailbreak踩坑
官方网站: https://unc0ver.dev/ 目前版本: 5.3.1 使用mac os+altstore步骤: AltStore Download AltStore. Use the link ...
- 自己常用的CMake用法总结
欢迎指正 CMake : A.download : https://cmake.org/download/ B.tutorial: https://cmake.org/cmake-tutorial/ ...
- 【九度OJ】题目1197:奇偶校验 解题报告
[九度OJ]题目1197:奇偶校验 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1197 题目描述: 输入一个字符串,然后对每个字符 ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 基于CA认证(结合文档在线预览)的电子签章解决方案
分享一个基于CA认证(结合文档在线预览)的电子签章实现思路,恰巧是最近项目中遇到的,欢迎大家一起讨论. 一. 项目背景 在公司业务系统中,按照传统的签章方式,存在以下痛点: 1.成本高,体现在纸质合同 ...
- 人脸搜索项目开源了:人脸识别(M:N)-Java版
一.人脸检测相关概念 人脸检测(Face Detection)是检测出图像中人脸所在位置的一项技术,是人脸智能分析应用的核心组成部分,也是最基础的部分.人脸检测方法现在多种多样,常用的技术或工具大 ...
- Django项目部署到Apache服务器上
之前写了把Django部署到XAMPP上,但是有bug,翻apache日志的时候发现会无法import _ssl,然后我就怒而直接装apache2了 配置方法大约和这篇文章差不多 安装必要的包 sud ...