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/ ...
随机推荐
- 转载:SQL 字符串操作函数
http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 以下所有例子均Studnet表为例: 计算字符串长度len()用来 ...
- shell 获取文件名
1.获取文件名并修改文件名 2.$@ 遍历参数 3.赋值要加"" 4.if 判断注意空格 else后面不能跟then
- <C#入门经典>学习笔记1之初识C#
序言 选择< C#入门经典第五版>作为自学书籍,以此记录学习过程中的笔记与心得. C#简单介绍 1. C#是一种块结构的语言 2. C#区分大写和小写 C#变量 C#的变量定义与C语言相似 ...
- Android 浏览器文本垂直居中问题
问题描述 在开发中,我们常使用 line-height 属性来实现文本的垂直居中,但是在安卓浏览器渲染中有一个常见的问题,就是对于小于12px的字体使用 line-height 属性进行垂直居中的时候 ...
- html5小趣味知识点系列(一)autofocus
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 移动端实用的meta标签
直接上代码,代码自有颜如玉 代码自有黄金屋啊 <meta http-equiv="Content-Type" content="text/html; charset ...
- jqury 如何获取 kindeditor 中textarea 的值
获取文本内容,可是的创建时怎么也不能获取,利用FF的firebug查看到自己所写的内容在一个iframe中,于是想从iframe中获取文本,想要用 $(“ifame”).html();获取内容,可是依 ...
- IOS程序国际化
1.1 新建一个Single View app模版项目,命名为Localization. 1.2 新建后,可以看到工作目录结构文件如下,单击InfoPlist.strings,查看右边的属性,在Loc ...
- c/c++的一些小知识点3
---恢复内容开始--- ---恢复内容结束---
- data standardization
import random import numpy as np l, num, gen_min_, gen_max_ = [], 100, 1, 200 l = [random.randint(ge ...