Leetcode 162 Find Peak Element 查找峰值元素(极大值)

这篇博客介绍了LeetCode 162题的三种解法,包括暴力遍历、改进的线性扫描以及利用二分查找在对数时间内找到峰值元素。博主详细解释了每种解法的思路,并提供了相应的C语言实现代码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题地址

https://leetcode.com/problems/find-peak-element/

题目描述

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.
给出一个不含重复元素的数组num,找出其中的峰值元素并返回其索引(下标)值.

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] = -∞.
假设nums[-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.
例如,在数组 [1, 2, 3, 1] 中,3是峰值元素,你的函数需要返回下标2.

spoilers.
挑战.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值