首页 > 其他分享 >162. Find Peak Element

162. Find Peak Element

时间:2024-06-10 15:58:02浏览次数:17  
标签:index return nums int Element middle element Find 162

A peak element is an element that is strictly greater than its neighbors.

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

You must write an algorithm that runs in O(log n) time.

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: 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.

Constraints:

  • 1 <= nums.length <= 1000
  • -231 <= nums[i] <= 231 - 1
  • nums[i] != nums[i + 1] for all valid i.

注意:

        1.明确一点,当middle不是峰值时,无论左边还是都应该会有一个峰值

        2.这里用到了一个lambda表达式,用来处理边界的情况,因为这里要比左右两边都大,要处理刚好在最左边和最右边的情况

        3.二分查找法:(1)当middle为峰值时,更新ret的值,并break,最后返回

                                 (2)如果middle对应的值比middle+1对应的大,就往右,否则往左。

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        auto get=[&](int i)->pair<int,int>{
            if(i==-1 || i==nums.size()){
                return {0,0};
            }
            return {1,nums[i]};
        };
        int left=0;
        int right=nums.size()-1;
        int ret=-1;
        while(left<=right){
            int middle=left+(right-left)/2;
            if(get(middle-1)<get(middle) && get(middle+1)<get(middle)){
                ret=middle;
                break;
            }else if(get(middle)>get(middle-1)){
                left=middle+1;
            }else{
                right=middle-1;
            }
        }
        return ret;
    }
};

标签:index,return,nums,int,Element,middle,element,Find,162
From: https://blog.csdn.net/2301_80161204/article/details/139577287

相关文章

  • 如何快速入门Element-UI:打造高效美观的前端界面
    Element-UI是一款基于Vue.js的开源组件库,提供了丰富的UI组件,可以帮助开发者快速构建美观、响应式的前端界面。本文将详细介绍如何快速入门Element-UI,包括环境搭建、组件使用、样式定制及常见问题解决方法,帮助你高效地使用Element-UI进行前端开发。一、环境搭建1.准......
  • Could not find artifact com.mysql:mysql-connector-j:pom:8.0.36 in central (https
    遇到修改依赖项的MySQL版本结果说找不到依赖项解决方法确保MySQL版本正确降低依赖项的MySQL版本,修改后更新即可以我的MySQL版本举例,可以降低MySQL版本到依赖项支持的版本<dependency><groupId>com.mysql</groupId><artifactId>m......
  • element-ui el-table表格固定表头代码示例
    在使用element-ui的el-table组件时,固定表头是一个常见的需求,特别是当表格内容较多时。以下是固定表头的一个基本示例代码:<template><el-table:data="tableData"style="width:100%"height="300px"><el-table-columnprop="date"label="日期"......
  • elementary OS 8的新消息
    原文:HappyPride!HaveSomeUpdates!⋅elementaryBlog这个月,我们为OS7带来了一些意外惊喜,包括GNOME应用的新版本和邮件应用的重大更新。Wayland也来了,我们有了一种新的方式来管理驱动程序,并且我们现在默认提供Flathub!别忘了,平台8现在已经准备好迎接开发者了。继续阅读,了......
  • 【已解决】Python报错 ERROR: Could not find a version that satisfies the requirem
    本文摘要:本文已解决ERROR:Couldnotfindaversionthatsatisfiestherequirement的相关报错问题,并总结提出了几种可用解决方案。同时结合人工智能GPT排除可能得隐患及错误。......
  • Element UI上传图片和PDF,支持预览,并支持复制黏贴上传
      背景 如上图,使用ElementUI的el-upload组件,并且预览的时候可以展示图片和PDF格式文件; 做法 index.vue<template><div><el-uploadv-model="diaForm.list":limit="5":on-exceed="handleExceed":on-preview=&q......
  • vue动态加载组件import引入组件找不到组件(Error: Cannot find module)
    更多ruoyi-nbcio功能请看演示系统gitee源代码地址前后端代码:https://gitee.com/nbacheng/ruoyi-nbcio演示地址:RuoYi-Nbcio后台管理系统http://218.75.87.38:9666/更多nbcio-boot功能请看演示系统 gitee源代码地址后端代码:https://gitee.com/nbacheng/nbcio-boot前端......
  • Element Plus el-tooltip嵌套el-popover
    1.场景需求在按钮中鼠标移入显示Tooltip文字提示,点击显示Popover提示框2.实现el-tooltip与el-popover之间需要有一层dom元素<template><el-tooltipeffect="dark":show-after="500"content="功能开发"placement="right"><spanref="dom&qu......
  • [CSS] Use CSS Grid to Animate Elements with Dynamic Height
    Currentlyweareanimatingtoafixedheightof100pxonhoversincebrowserscan'tanimatefrom0to auto:<pclass="h-0overflow-hiddentext-white/70transition-allgroup-hover:h-[100px]"> Loremipsumdolorsit,ametconsecteturad......
  • can not find lambda cache for this property [] of entity
    这个错误通常出现在使用Hibernate或者类似ORM框架时,当框架尝试缓存一个实体的属性,但是找不到对应的缓存时。这个错误表明框架无法为实体中名为adnm的属性找到合适的缓存机制。解决这个问题的方法通常包括以下几个步骤:确认该属性是否确实存在于实体中,并且已经正确地注解了。如果......