Leecode 162. Find Peak Element

2023-10-19 05:58
文章标签 find element 162 peak leecode

本文主要是介绍Leecode 162. Find Peak Element,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

可运行

​
class Solution {
public:int findPeakElement(vector<int>& nums) {return findPeakElementCore(nums, 0, nums.size() - 1);}
private:int findPeakElementCore(vector<int>& nums, int beg, int end) {if (end == beg) return beg;if (end - beg == 1){if(nums[end] < nums[beg]) return beg;else return end;} int mid = beg + (end - beg) / 2;if (nums[mid] > nums[beg] && nums[mid] > nums[end]) {if (nums[mid] < nums[mid + 1]) return findPeakElementCore(nums, mid + 1, end);else return findPeakElementCore(nums, beg, mid);}if (nums[beg] > nums[beg + 1]) {return beg; }if (nums[end] > nums[end - 1]) {return end;}return findPeakElementCore(nums, beg + 1, end - 1);}};​

参考后

class Solution {
public:int findPeakElement(vector<int>& nums) {return findPeakElementCore(nums, 0, nums.size() - 1);}
private:int findPeakElementCore(vector<int>& nums, int beg, int end) {if (end == beg) return beg;int mid = beg + (end - beg) / 2;if (nums[mid] < nums[mid + 1]) return findPeakElementCore(nums, mid + 1, end);return findPeakElementCore(nums, beg, mid);}};

 

这篇关于Leecode 162. Find Peak Element的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/237773

相关文章

Linux find 命令完全指南及核心用法

《Linuxfind命令完全指南及核心用法》find是Linux系统最强大的文件搜索工具,支持嵌套遍历、条件筛选、执行动作,下面给大家介绍Linuxfind命令完全指南,感兴趣的朋友一起看看吧... 目录一、基础搜索模式1. 按文件名搜索(精确/模糊匹配)2. 排除指定目录/文件二、根据文件类型筛选三、时间

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

element-ui下拉输入框+resetFields无法回显的问题解决

《element-ui下拉输入框+resetFields无法回显的问题解决》本文主要介绍了在使用ElementUI的下拉输入框时,点击重置按钮后输入框无法回显数据的问题,具有一定的参考价值,感兴趣的... 目录描述原因问题重现解决方案方法一方法二总结描述第一次进入页面,不做任何操作,点击重置按钮,再进行下

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

leetcode#496. Next Greater Element I

题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:

element-ui打包之后图标不显示,woff、ttf加载404

1、bug 起因 昨天在 vue 项目中编写 element-ui 的树形结构的表格,发现项目中无法生效,定位问题之后发现项目使用的 element-ui 的版本是 2.4.11 。看了官方最新版本是 2.15.14,然后得知 2.4.11 版本是不支持表格树形结构的。于是决定升级 element-ui 的版本,方便后续的开发。 升级之后本地简单的过了一遍系统功能,并没有发现有什么不妥,于