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. VS2015编译TIFF3.8.0源码

    没有CMakeLists.txt,不能使用CMake GUI了.源文件中有makefile.vc,所以使用nmake 进入VS2015命令行 nmake -f makefile.vc 修改nmake. ...

  2. Linux的网卡相关

    检测linux下网卡是否正常 1.dmesg | grep eth 如果出现以下  eth0: link up 说明是网卡正常的 eth0: registered as PCnet/PCI II 79 ...

  3. 查询软件和硬件列表清单[将文章里代码另存为 list.vbs,双击运行就会出现一个html页面]

    '==========================================================================' Name: 查询软件和硬件列表清单' 不支持W ...

  4. 《C++ Primer Plus》第14章 C++中的代码重用 学习笔记

    C++提供了集中重用代码的手段.第13章介绍的共有继承能够建立is-a关系,这样派生类可以重用基类的代码.私有继承和保护继承也使得能够重用基类的代码,单建立的是has-a关系.使用私有继承时,积累的公 ...

  5. eclispe创建gradle项目

    1.打开eclipse,选择Help——>install from Catalog,安装如图所示的gradle 2.右击空白处,new——>other——>Gradle——>G ...

  6. ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载

    ExtJS6.0+快速入门+API下载地址 ExtAPI 下载地址如下,包含各个版本 http://docs.sencha.com/misc/guides/offline_docs.html 1.使用 ...

  7. MYSQL系列之(一)

      mysql简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不 ...

  8. 网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP

    TUN 设备 TUN 设备是一种虚拟网络设备,通过此设备,程序可以方便得模拟网络行为.先来看看物理设备是如何工作的:

  9. 11.事件驱动events

    事件驱动events ==> events.EventEmitter, EventEmitter 的核心就是事件发射与事件监听器功能的封装更详细的 API 文档参见 http://nodejs. ...

  10. 【转】DevOps的前世今生

    转自:http://www.infoq.com/cn/news/2016/09/learn-devops-from-reports 目前在国外,互联网巨头如Google.Facebook.Amazon ...