leetcode162. 寻找峰值(二分法)

峰值元素是指其值大于左右相邻值的元素。

给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。

数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。

你可以假设 nums[-1] = nums[n] = -∞。

示例 1:

输入: nums = [1,2,3,1]
输出: 2
解释: 3 是峰值元素,你的函数应该返回其索引 2。

代码

class Solution {public int findPeakElement(int[] nums) {int l=1,m=nums.length,r=m-1;while (l<=r){int mid=(r-l)/2+l;if(nums[mid]<=nums[mid-1])判断位于峰值的上升还是下降缘r=mid-1;else l=mid+1;}return r;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393431.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python网络爬虫(5)BeautifulSoup的使用示范

创建并显示原始内容 其中的lxml第三方解释器加快解析速度 import bs4 from bs4 import BeautifulSoup html_str """ <html><head><title>The Dormouses story</title></head> <body> <p class"title"><…

Mingw编译DLib

Mingw编译DLib 因为机器上安装了qt-opensource-windows-x86-mingw530-5.8.0&#xff0c;所以准备使用其自带的mingw530来编译DLib使用。 因为DLib使用CMake的构建脚本&#xff0c;所以还请先安装好CMake。 cmake的下载地址如下https://cmake.org/files/v3.7/cmake-3.7.2-win64-…

探索JavaScript的关闭功能

Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!“发现功能JavaScript”被BookAuthority评为最佳新功能编程书籍之一 &#xff01; A closure is an inner function that has access to the outer scope, even…

QueryList 配置curl参数 的文档位置 QueryList抓取https 终于找到了

需要设置ssl证书&#xff0c;或者不验证证书&#xff0c;例&#xff1a;$ql QueryList::get(https://...,[],[verify > false]);设置这个 verify > false , 所以curl的其他参数就在这里配置即可 文档在 https://guzzle-cn.readthedocs.io/zh_CN/latest/request-optio…

leetcode981. 基于时间的键值存储(treemap)

创建一个基于时间的键值存储类 TimeMap&#xff0c;它支持下面两个操作&#xff1a; set(string key, string value, int timestamp) 存储键 key、值 value&#xff0c;以及给定的时间戳 timestamp。 2. get(string key, int timestamp) 返回先前调用 set(key, value, times…

物联网笔记

转载于:https://www.cnblogs.com/16-C-kai/p/6596682.html

关于大学生玩网络游戏的调查问卷

1.创建问卷&#xff0c;输入调查名称 2编辑问卷 3检查问卷&#xff0c;是否有误 4.提交并发布问卷 5分享问卷 6.问卷分析 转载于:https://www.cnblogs.com/dzw1996/p/7786754.html

java自动排序_java ArrayList自动排序算法的实现

前几天写的那个是错误的&#xff0c;在这里将正确的更新。。。通过实现ComParator接口&#xff0c;并且对Compare函数进行重写&#xff0c;自定义排序规则实现对ArrayList中对象的排序。。Student类定义&#xff1a;通过右键-》source-》自动生成Set和get方法package first;imp…

1到100的二进制编码_每天经过100天的编码后,我学到了什么

1到100的二进制编码Eleftheria Batsou is a web developer from Thessaloniki, Greece. She gave a talk at the Codegarden conference about her experience doing a solid 100 days of coding every day as part of the #100DaysOfCode Challenge.Eleftheria Batsou是来自希…

第六次 实验

转载于:https://www.cnblogs.com/P201821440005/p/10967987.html

leetcode658. 找到 K 个最接近的元素(二分法)

给定一个排序好的数组&#xff0c;两个整数 k 和 x&#xff0c;从数组中找到最靠近 x&#xff08;两数之差最小&#xff09;的 k 个数。返回的结果必须要是按升序排好的。如果有两个数与 x 的差值一样&#xff0c;优先选择数值较小的那个数。 示例 1: 输入: [1,2,3,4,5], k4,…

du命令、df命令用法

一、du命令 [plain] view plaincopy print?[rootwc1 mysql]# du --help Usage: du [OPTION]... [FILE]... or: du [OPTION]... --files0-fromF Summarize disk usage of each FILE, recursively for directories. Mandatory arguments to long options are mandatory…

mysql 循环创建列_mysql – 查询列中的循环值

我需要创建一个查询,一次只将一列的值移动一行↑&#xff1a;----------------------------| anotherCOL | values_to_loop |----------------------------| 1 | 1 || 2 | 2 || 3 | 3 || 4 | 4 || 5 | 5 || 6 | 6 || 7 | 7 || 8 | 8 || 9 | 9 || 10 | 10 |--------------------…

因子个数与因子和

题目&#xff1a;LightOJ:1341 - Aladdin and the Flying Carpet(因子个数&#xff09; Its said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was …

如何在JavaScript中直观地设计状态

by Shawn McKay肖恩麦凯(Shawn McKay) 如何在JavaScript中直观地设计状态 (How to visually design state in JavaScript) 使用状态机和状态图开发应用程序的路线图 (A roadmap for developing applications with state machines & statecharts) Why does state managemen…

SQL Server 2008 - Cannot set a credential for principal 'sa'.

很久没有用到SQL Server了&#xff0c;今天有幸在帮同事解决一个SQL Server数据连接的问题时突然发现我无法修改我的sa用户的密码了。过程是这样的&#xff1a;一开始我本地的数据库实例是Windows认证方式&#xff0c;我想将它改成Windows和数据库混合认证方式后用sa账户登录&a…

leetcode50. Pow(x, n)(快速幂)

实现 pow(x, n) &#xff0c;即计算 x 的 n 次幂函数。 示例 1: 输入: 2.00000, 10 输出: 1024.00000 代码 class Solution {public double myPow(double x, int n) {long tn;return t>0?Pow(x,t):1/Pow(x,-t);//判断幂指数}public double Pow(double x, long n) {if(n…

Java DES 加解密(DES/CBC/PKCS5Padding)

/*** DES加密** param data 加密数据* param key 密钥* return 返回加密后的数据*/public static byte[] desEncrypt(byte[] data, String key, String charset) {try {Cipher cipher Cipher.getInstance("DES/CBC/PKCS5Padding");byte[] k charset null || char…

mysql 连接池 100_mysql的最大连接数默认是100_MySQL

mysql的最大连接数默认是100, 这个数值对于并发连接很多的数据库应用是远远不够的&#xff0c;可以把它适当调大&#xff0c;mysql的最大连接数默认是100, 这个数值对于并发连接很多的数据库应用是远远不够的&#xff0c;可以把它适当调大&#xff0c;whereis safe_mysqld找到s…

angular上传图片_如何使用Angular轻松上传图片

angular上传图片by Filip Jerga由Filip Jerga 如何使用Angular轻松上传图片 (How to make image upload easy with Angular) This is the second part of the tutorial on how to upload an image to Amazon S3. You can find the first part here. In this article, we will …