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.

Note:

Your solution should be in logarithmic complexity.

思路:log time,所以用二分法。二分法无非是左值、中间值、右值的比较。

如果mid<mid-1,那么下一次判断left~mid-1;(此时mid相当于-∞)

如果mid<mid+1,那么下一次判断mid+1~right(此时mid相当于-∞)

class Solution {
public:
int findPeakElement(vector<int>& nums) {
return binarySearch(nums,,nums.size()-);
} int binarySearch(vector<int>& nums, int start, int end){
if(start == end) return start;
if(start+ == end){
if(nums[start]>nums[end]) return start;
else return end;
} int mid = start + (end-start)/;
if(nums[mid]<nums[mid-]){
return binarySearch(nums,start,mid-);
}
else if(nums[mid]<nums[mid+]){
return binarySearch(nums,mid+,end);
}
else{
return mid;
}
}
};

162. Find Peak Element (Array; Divide-and-Conquer)的更多相关文章

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

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

  2. LeetCode 162 Find Peak Element

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

  3. Java for LeetCode 162 Find Peak Element

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

  4. 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 --------- java

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

  6. LeetCode OJ 162. Find Peak Element

    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 (找到峰值)

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

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

  9. Find Peak Element(ARRAY - Devide-and-Conquer)

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

随机推荐

  1. idea 关闭自动保存,未保存星号提醒, springboot + freemarker 热部署

    1,自动保存 File > setting 去掉下图勾选 2,未保存文件星号提示 File > Settings 3,spring boot 项目 热部署 3.1,pom文件添加依赖 &l ...

  2. weblogic stage更改不马上生效

    在主机上domins中存在stage目录,且删除相关文件后页面访问报404 mc11>home/ap/user/domins/user/stage/project/project.war/js/ ...

  3. leetcode1005

    func largestSumAfterKNegations(A []int, K int) int { sort.Ints(A) var negatives int var zeros int va ...

  4. linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)

    代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...

  5. iOS多语言

    https://blog.csdn.net/huangmindong/article/details/53464334 App多语言,字符串统一放在 Localizable.strings 文件里. ...

  6. 把网卡中断绑定到CPU,最大化网卡的吞吐量(转)

    先来看一下问题, 我们通过 ifconfig 查看接口的名称 为 p15p1, 一般机器为 eth0 再通过命令 ➜ ~ cat /proc/interrupts | head -n 1 && ...

  7. 尚硅谷redis学习10-复制

    是什么? 能干嘛? 怎么玩? 1) 初始情况 设置slave 日志查看 主机查看 备机日志 复制状态 觉见问题 1 切入点问题?slave1.slave2是从头开始复制还是从切入点开始复制?比如从k4 ...

  8. C语言复习:字符串和一级指针

    字符串基本操作 字符数组初始化方法 int main() {     //1 {}号法 初始化列表     //数组初始化有2种方法 默认元素个数.指定元素个数     char buf1[] = { ...

  9. 【357】sorted 函数高级用法

    参考:Python 内置函数sorted()在高级用法 - Brad1994 - 博客园 sorted 函数主要实现的就是对于可迭代对象进行排序,对于一维数据排序很好理解与实现,直接调用即可,本文主要 ...

  10. Structs复习 字符编码问题

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" ...