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. Loadrunner windows计数器

    object (对象) Counters (计数器名称) Description (描述) 参考值 Memory Available Mbytes 可用物理内存数.如果该值很小(4MB或更小),则说明 ...

  2. hadoop错误各种原因

    NoRouteToHostException 错误描述: INFO hdfs.DFSClient: Exception in createBlockOutputStream java.net.NoRo ...

  3. PHP遍历文件夹及子文件夹所有文件(此外还有飞递归的方法)

    <html> <body> <?php function traverse($path = '.') { $current_dir = opendir($path); / ...

  4. 解决 ssh 登录到ubuntu server 慢的问题

    最近在服务器上使用ubuntu系统,服务器上开启sshd服务,在客户端处使用ssh登录到服务器的时候出现卡顿的现象. 在网上搜索了解决方案,主要是: 1. 修改服务器的 /etc/ssh/sshd_c ...

  5. Excel 2010 对号叉号怎么打出来

    按小键盘数字键:Alt+41420  对号 按小键盘数字键:Alt+41642  叉号 http://jingyan.baidu.com/article/fdbd4277c228cdb89e3f482 ...

  6. node中的对象

    1. class的概念 定义一个class,属性都是private,方法都是public. Hello.js: 使用class index.js: 2. 单例类 使用exports而不是module. ...

  7. linux的ls命令输出结果的逐条解释

    转自:http://blog.csdn.net/god123209/article/details/7193485 ls 命令的含义是list显示当前目录中的文件名字.注意不加参数它显示除隐藏文件外的 ...

  8. java父类可以强制转化成子类吗?

    转自:http://blog.csdn.net/ld422586546/article/details/9707997 Java中父类强制转换成子类的原则:父类型的引用指向的是哪个子类的实例,就能转换 ...

  9. java设计模式----外观模式(门面模式)

    外观模式主要应用场景在于为复杂的子系统提供一个简单的接口,提高子系统的独立性. 创建DrawerOne类: package facade; public class DrawerOne { publi ...

  10. [shell]用shell脚本将本地文件夹与ftp上的文件夹同步

    需求说明 最近在AIX上做开发,开发机器在office网段,测试机器在lab网段,不能互相通讯,只能通过特定的ftp来传文件. 每次上传的机器都要做:登录ftp,进入我的目录,上传:下载的机器都要做: ...