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.

双指针:

class Solution {
public:
int findPeakElement(vector<int>& a) {
int i = 0;
int j = a.size() - 1;
if (j==0) return 0;
while(i<j) {
if (a[i]<a[j]) {
i++;
} else {
j--;
}
}
return i;
}
};

  

 class Solution {
public int findPeakElement(int[] nums) {
int n = nums.length;
int lo = 0;
int hi = n - 1;
while(hi>lo){
int mid = lo + (hi - lo)/2;
if(nums[mid]<nums[mid+1])
lo = mid + 1;
else
hi = mid;
}
return lo;
}
}

162. Find Peak Element(二分查找 )的更多相关文章

  1. Find Peak Element——二分查找的变形

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. 【LeetCode】162. Find Peak Element 解题报告(Python)

    [LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...

  3. (二分查找 拓展) 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 ...

  4. [LeetCode] 162. Find Peak Element 查找峰值元素

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  5. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  6. ✡ 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] ≠ ...

  7. 【LeetCode】162. Find Peak Element (3 solutions)

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  8. 【刷题-LeetCode】162 Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  9. leetcode 162 Find Peak Element(二分法)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

随机推荐

  1. RMQ LAC 入门

    RMQ RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大) ...

  2. js 获取图片url的Blob值并预览

    1)使用 XMLHttpRequest 对象获取图片url的Blob值 //获取图片的Blob值 function getImageBlob(url, cb) { var xhr = new XMLH ...

  3. ZooKeeper(五)-- Curator使用

    前言 Curator是Netflix开源的一套ZooKeeper客户端框架: 1.封装ZooKeeper client与ZooKeeper server之间的连接处理; 2.提供了一套Fluent风格 ...

  4. kubectl get 输出格式

    常见的输出格式有: * custom-columns=<spec> # 根据自定义列名进行输出,逗号分隔 * custom-columns-file=<filename> # ...

  5. m2014-architecture-imgserver->利用Squid反向代理搭建CDN缓存服务器加快Web访问速度

    案例:Web服务器:域名www.abc.com IP:192.168.21.129 电信单线路接入访问用户:电信宽带用户.移动宽带用户出现问题:电信用户打开www.abc.com正常,移动用户打开ww ...

  6. PyQt4 菜单栏 + 工具栏 + 状态栏 + 中心部件 生成一个文本编辑部件示例

    我们将创建一个菜单栏.一个工具栏.一个状态栏和一个中心部件. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import ...

  7. MVC AJAX Pager Helper

    MVCPager 分页控件: Author:杨涛 http://www.webdiyer.com/mvcpager/demos/ajaxpagers/ https://yunpan.cn/cq4HDc ...

  8. MUI 单个图片上传预览(拍照+系统相册):先选择->预览->上传提交

    1 html部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  9. CSS-自定义高度的元素背景图如何自适应以及after伪元素在ie下的处理

    我都好久没更新了! 遇到一个效果,之前没有考虑清楚,设置了固定高度,到了后边,产品要加长,我就觉得设计得从新弄张长点的背景图!这不多余么? 其实分析原图还是可以再切分,再细化到不用改设计图,让我们前端 ...

  10. GMT时间转换为当地时间的方法

    1.取得当地时间与GMT时间的时间差 (new Date()).getTimezoneOffset()  //单位为分钟 2.GMT时间加上与当地时间的时间差 (new Date(GMTTime)) ...