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. HTML5 Canvas 小例子 伸缩旋转方块

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 01.制作ico图标的工具

    制作ico图标的工具在线转换地址: http://lvwenhan.com/convertico/ http://lvwenhan.com/convertico/Converticon.swf

  3. tensorflow实战系列(三)一个完整的例子

    #!/usr/bin/env python2# -*- coding: utf-8 -*-"""Created on Wed Jan 18 08:42:55 2017 @ ...

  4. Windows环境安装Django步骤

    前提:已经安装Python 1.先从Django官网下载压缩包:https://www.djangoproject.com/download/ 2.解压Django,如我解压到 D:\Python\D ...

  5. UI5-学习篇-16-云端SCP-Destination配置

    1.登录路径: https://account.ap1.hana.ondemand.com/#/home/welcome 2.查看云连接器 如下图所示,虚拟主机都已连接且资源可用: 若虚拟主机连接存在 ...

  6. 函数参数,const 引用 和 非 const引用是不同的函数。

    举个例子, void f(const int &x) 和 void f(int &x) 是不同的函数. 函数的返回值不能作为区分

  7. Java继承与多态浅析

    一.继承 1.通过extends继承的父类可以是不加abstract关键字的普通类,也可以是加了abstract关键字的抽象类.继承普通类时可以覆写父类的方法,或者创建自己独有的方法,或者这两     ...

  8. angular的启动原理

    当你用浏览器去访问index.html的时候,浏览器依次做了如下一些事情: 加载html,然后解析成DOM: 加载angular.js脚本:加载完成后自执行,生成全局angular对象,监听DOMCo ...

  9. Linux下使用命令行配置IPMI

    ipmitool是什么: 百度百科给的解释已经够用了,简单说就是“IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备“智能 ...

  10. MyBatis基础-1

    1.Mybatis简介 2.Mybatis环境搭建 3.Mybatis的开发方式 一.什么框架 框架其本质是半成品程序,提供相关规范,并且提供大量可重用的组件. 目的:让开发者开发出结构比较良好,可读 ...