根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号。并且要求时间复杂度为logn

分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历。又因为只需要找到其中的一个峰值,那么,每次对半分,便可以达到logn的复杂度。

   根据对半分的思路继续想,不难发现只要确定了中间位置的数是处在上升阶段还是下降阶段,就可以确定在某一侧必有一个峰值。

   往复多次,即可找出两个点,其中一个一定处于某一个峰值上。

java代码:

 public int findPeakElement(int []nums)
{
int left=0;
int right=nums.length-1;
if(right==0) return 0;
while(left<right-1)
{
int mid=(left+right)/2;
if (nums[mid]<nums[mid+1])//上升
{
left=mid+1;
}
else//下降
{
right=mid;
}
}
return nums[left]>nums[right]?left:right;
}

python代码:

def findPeakElement(self, nums):
length=len(nums)
if length==1:
return 0
left=0
right=length-1
while left<right-1:
mid=(left+right)/2
#increase
if nums[mid]<nums[mid+1]:
left=mid+1
#decrease
else:
right=mid
return left if nums[left]>nums[right] else right

leetcode 日记 162. Find Peak Element java python的更多相关文章

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

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

  2. ✡ 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] ≠ ...

  3. 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] ≠ ...

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

  5. 【刷题-LeetCode】162 Find Peak Element

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  6. 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] ≠ ...

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

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

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

随机推荐

  1. what is difference in (int)a,(int&)a,&a,int(&a) ?

    This interview question come from a famous communication firm of china. : ) #include <iostream> ...

  2. Nodejs的安装及配置

    1.从Nodejs官网下载安装包,进行安装,是否安装成功,可以从cmd npm去检查,出现如下界面,就表示安装成功 2.打开Webstorm进行配置,(如果安装前打开webstorm了,需要重启web ...

  3. Redis教程(四):Hashes数据类型

    转自:http://www.jb51.net/article/65240.htm 一.概述: 我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器 ...

  4. 转:图解Git[强烈推荐]

    https://my.oschina.net/xdev/blog/114383 再次感谢原著作者和中文翻译者. 此页图解git中的最常用命令.如果你稍微理解git的工作原理,这篇文章能够让你理解的更透 ...

  5. 【积累】发送验证码按钮倒计时js

    注册的时候要发送验证码,就上网研究了一下,写了一个简单点的... jsp页面: <input type="button" id="testbtn" val ...

  6. opencv vs环境配置

    1.新建环境变量 OPENCV = D:\Programss\opencv\build 2.%OPENCV%\x86\vc12\bin;加入Path环境变量 3.导入 VS属性表文件 cv2413.p ...

  7. linux建立一个快捷方式,连接到另一个目录

    sudo ln -s 源目录 目标快捷方式比如你要在/etc下面建立一个叫LXBC553的快捷方式,指向/home/LXBC,那就是sudo ln -s /home/LXBC /etc/LXBC553

  8. VS中Qt的探索02

    边看C++ GUI QT4教程,边在VS2010中进行编程学习探索. 在使用Qt设计师时,其中每一个对象的ObjectName属性是非常重要的,在程序功能的实现过程中,需要不断的使用该变量名. 当所有 ...

  9. openssl evp RSA 加密解密

    openssl evp RSA 加密解密 可以直接使用RSA.h 提供的接口 如下测试使用EVP提供的RSA接口 1. EVP提供的RSA 加密解密 主要接口: int EVP_PKEY_encryp ...

  10. iOS多线程

    关于iOS多线程 概述 这篇文章中,我不会说多线程是什么.线程和进程的区别.多线程有什么用,当然我也不会说什么是串行.什么是并行等问题,这些我们应该都知道的. 在 iOS 中其实目前有 4 套多线程方 ...