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. python2.0_day18_Django自带的用户认证模块的使用

    用户验证我们基于一个项目来学习利用Django框架中的user表实现用户认证Django练习小项目:学员管理系统设计开发 项目需求: 1.分讲师\学员\课程顾问角色, 2.学员可以属于多个班级,学员成 ...

  2. docker中文、手册、教程

    Docker资源 Docker官方英文资源: docker官网:http://www.docker.com Docker windows入门:https://docs.docker.com/windo ...

  3. 如果我们的MCU调试不通过,我们该如何测试?(未完待续)

    我们拿到一款MCU,首先在选型的时候对他们都会有一个大概的了解,首先我们知道它多大与的FLASH,多大的RAM,是否与我们的项目适合,资源上是否足够,内频是否足够,运行速率能否达到标准,性能怎么样,比 ...

  4. JDK1.8在LINUX下安装步骤

    JDK1.8在LINUX下安装步骤: 在/usr/lib/目录下新建jvm文件夹,如果已有jvm文件夹,则将之前的JDK版本删除,即在jvm目录下执行命令:rm –rf * 将JDK文件jdk-8u4 ...

  5. 【Linux】 ftp 主动被动模式

    LNMP 搭建得服务器,在使用ftp时候,报如下错误: 经查,是ftp 主动模式被动模式问题 工具:  Xftp5   ,把被动模式勾 取消 (其他客户端可以网上查一下 相应的 被动模式转主动模式设置 ...

  6. 【Redis】php+redis实现消息队列

    在项目中使用消息队列一般是有如下几个原因: 把瞬间服务器的请求处理换成异步处理,缓解服务器的压力 实现数据顺序排列获取 redis实现消息队列步骤如下: 1).redis函数rpush,lpop 2) ...

  7. 【mysql】mysql front 提示Access violation at address 010C9CD0 in module ‘mysql-front.exe’

    1 错误描述: 利用mysql-front 工具新建数据库.提示了一下错误 2 解决办法: 内存越界问题,最好重新注册下Windows的动态链接库 首先“开始”—“cmd” 在打开的dos窗口中运行

  8. img通过canvas转成base64编码

    <script type="text/javascript"> function getBase64Image(img) { var canvas = document ...

  9. Linux环境下安装Redis并完成测试(已验证)

    准备工作: 本人测试环境:Win7 xshell远程登录Linux Linux: ubuntu 软件包:redis-3..2.6.tar.gz (Linux下redis安装包) =========== ...

  10. 160225、解决纯js文件国际化的问题

    1.js中国际化 function test(){     alert("<s:text name='jsp.loading'/>"); }   2.最近在做strut ...