leetcode 162 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.
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] = -∞
.
For example, in array [1, 2, 3, 1]
, 3 is a peak element and your function should return the index number 2.
题解:二分。从mid开始找,找到了就返回结果。如果mid不是,哪边比mid大,结果必然在那一边的序列里面。
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n=nums.size();
nums.push_back(numeric_limits<int>::min());
int l=,r=n;
int mid;
while(l<r){
mid=(l+r)/;
if((mid==&&nums[mid]>nums[])||(nums[mid-]<nums[mid]&&nums[mid+]<nums[mid])){
return mid;
}
else if(nums[mid-]>nums[mid]){
r=mid;
}
else{
l=mid+;
}
}
return mid;
}
};
leetcode 162 Find Peak Element(二分法)的更多相关文章
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- LeetCode 162.Find Peak Element(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Java for LeetCode 162 Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- ✡ leetcode 162. Find Peak Element --------- java
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- Java学习篇之---json-lib(Maven)
json-lib(Maven) java中用于解释json的主流工具有org.json.json-lib与gson.本篇文章介绍json-lib. 项目中要用到json-lib.在pom.xml文件里 ...
- ios cocoapods获取不到最新库的原因主要有两个:
获取不到最新库的原因主要有两个: 1.cocoapods的版本过低 2.还没有更新本地仓库 解决方法: 1.更新pods 在终端执行命令行 sudo gem install cocoapods 执行完 ...
- python 微信跳一跳进阶
上一篇是通过图片识别来计算跳的距离,再计算按压时间,最后通过adb来控制手机跳的 本篇讲的是通过机器学习,来训练的算法进行跳一跳的 链接: github:https://github.com/Prin ...
- 使用Swoole加速Laravel(正式环境中)
1 Laravel的速度瓶颈在哪? 1.1 已有的一些优化方法 1.1.1 laravel官方提供了一些优化laravel的优化方法 php artisan optimize php artisan ...
- urllib.urlencode() 无法encode中文, UnicodeEncodeError
urllib.urlencode() 无法encode中文, UnicodeEncodeError, 具体错误内容如下:File "/System/Library/Frameworks/Py ...
- C#获取当前时间的各种格式
C#获取当前时间的各种格式 DateTime.Now.ToShortTimeString() DateTime dt = DateTime.Now; dt.ToString();//2005 ...
- Ajax之基础总结
一.Ajax 简介 Ajax 由 HTML.JavaScript技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.在详细探讨 Ajax 是 ...
- data standardization
import random import numpy as np l, num, gen_min_, gen_max_ = [], 100, 1, 200 l = [random.randint(ge ...
- Flask 进阶session和上下文管理解析
session的源码流程 将session放置在redis中 安装 pip install flask-session 使用 import redis from flask import Flask, ...
- android中handler和bundle有什么区别和联系 都是用来传递消息吗都是信息的载体吗
1.handler是消息处理者,通常重写Handler的handleMessage()方法,在方法中处理接收到的不同消息,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ha ...