Leetcode-162. Find Peak Element 寻找峰值 (二分查找) -python

该博客主要介绍了LeetCode 162题的解决方案,使用二分查找算法在给定数组中找到峰值元素(大于其相邻元素的值),并返回其索引。在数组nums[i] ≠ nums[i+1]的条件下,当存在多个峰值时,返回任意一个峰值的索引即可。示例中给出输入数组[1,2,1,3,5,6,4],峰值元素可能是2或6,对应的索引为1或5。博客内容包括问题描述、二分查找思路和算法实现,以及时间复杂度O(logn)的分析。" 107831301,9966373,单片机事件池实现与应用,"['嵌入式', '单片机']
摘要由CSDN通过智能技术生成

题目

峰值元素是指其值大于左右相邻值的元素。
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。
数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞。
链接:https://leetcode.com/problems/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:

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.

思路及代码

二分
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值