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. MVC源码

    http://aspnetwebstack.codeplex.com/ MVC源码

  2. Access数据操作-01

    1.未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 在菜单 “项目”的最下面 工程属性  菜单,选择“生成”选项卡,将目标平台由“Amy CPU”或者“*64”改成“* ...

  3. laravel 使用 vue (gulp)

    1)首先要安装 gulp 看这里 http://www.cnblogs.com/tujia/p/6397779.html 2)编辑js 默认 laravel 里有一个 /resources/asset ...

  4. 什么是“类数组对象”,在jquer中怎样将类数组对象转换为数组对象

    类数组对象的定义: 所谓"类数组对象"就是一个常规的Object对象,如$("div")但它和数组对象非常相似:具备length属性, 并以0.1.2.3……等 ...

  5. android编译make错误——"javalib.jar invalid header field”、"classes-full-debug.jar 错误 41 "

    错误:读取 out/target/common/obj/JAVA_LIBRARIES/core-tests_intermediates/javalib.jar 时出错:invalid header f ...

  6. 日记整理---->2016-11-01

    这里我们整理一下项目的流程,一般来说做一个模块之前.会有需求文档.页面原型和接口文档. 一. js获取radio的值 页面的html代码: <ul class="list-group& ...

  7. synchronized将任意对象作为对象监视器

    多个线程调用同一个对象中的不同名称的synchronized同步方法或synchronized(this)同步代码块时,调用的效果就是按顺序执行,也就是同步的,阻塞的.这说明synchronized同 ...

  8. [UML]UML 教程 - 第二部分

    UML作为软件开发典型的开发过程 业务过程模型创建 业务过程模型被用来定义发生在企业或组织内部的高级业务活动和业务过程,并且是建立用例模型的基础.一般来说业务过程模型比一个软件系统所能实现的更多(比如 ...

  9. 无法远程访问Mysql

    1.故障状态 [root@server02 ~]# mysql -utuser -h192. -p Enter password: ERROR (HY000): Can't connect to My ...

  10. AVG

    AVG([ DISTINCT | ALL ] expr) [ OVER(analytic_clause) ] SELECT MANAGER_ID,           LAST_NAME,       ...