题目

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.

代码

分别用递归和非递归两种方法实现。

非递归版代码:oj测试通过 Runtime: 55 ms

 class Solution:
# @param num, a list of integer
# @return an integer
def findPeakElement(self, num):
if len(num) == 1 :
return 0
if len(num) == 2 :
return [0,1][num[0] < num[1]]
start = 0
end = len(num)-1
while start <= end:
if start == end:
return start
if start+1 == end:
return [start,end][num[start] < num[end]]
mid = (start+end)/2
if num[mid] < num[mid-1]:
#start = 0
end = mid -1
elif num[mid] < num[mid+1]:
start = mid+1
#end = len(num)-1
else:
return mid

递归代码:oj测试通过 Runtime: 51 ms

 class Solution:
# @param num, a list of integer
# @return an integer
def find(self,num,start,end):
if start == end :
return start
if end == start + 1:
if num[start] < num[end] :
return end
else:
return start
mid = (start+end)/2
if num[mid] < num[mid-1] :
return self.find(num, start, mid-1)
if num[mid] < num[mid+1] :
return self.find(num, mid, end)
return mid def findPeakElement(self, num):
if len(num) == 1:
return 0
return self.find(num, 0, len(num)-1)

思路

二分查找思路升级版。参照如下两篇日志的思路:

http://bookshadow.com/weblog/2014/12/06/leetcode-find-peak-element/

http://blog.csdn.net/u010367506/article/details/41943309

这道题在理解题意上需要注意就是默认这个数组的虚头和虚尾都是负无穷,并且这个数组不存在相等的两个元素,这样就一定能够找到题中描述的peak point.

leetcode 【 Find Peak Element 】python 实现的更多相关文章

  1. [LeetCode] Find Peak Element 求数组的局部峰值

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

  2. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  3. LeetCode Find Peak Element [TBD]

    说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...

  4. LeetCode Find Peak Element 找临时最大值

    Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...

  5. LeetCode: Find Peak Element 解题报告

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

  6. [LeetCode] Find Peak Element 二分搜索

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

  7. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

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

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

  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. sql server 搭建发布订阅后,改端口不正常工作的问题

    sql 的发布订阅,想必大家都了解,但一般都是在默认的1433的情况下搭建的,那么1433换成别的端口,发布还能正常工作吗? 在一次客户的真实场景上我就遇到了. 好了,今天不想写太多,简化下, 测试环 ...

  2. linux 命令——25 linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...

  3. JavaScript: apply , call 方法

    我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里我做如下笔记,希望和大家 ...

  4. js中arr.sort的用法

    sort(sortfunction)为JS的数组对象(Array)的一个方法,提供排序功能 参数 sortFunction 为可选项,是用来确定排序原则的js函数, 这个函数有两个参数,分别代表每次排 ...

  5. Centos7安装 PostgreSQL步骤

    1. 安装服务器即可. Yum install postgresql-server Yum install postgresql-contrib 2. 验证是否安装成功: rpm -aq| grep ...

  6. IOS UITabBarController(控制器)的子控制器

    UITabBarController的简单使用 ● UITabBarController的使用步骤 ➢ 初始化UITabBarController ➢ 设置UIWindow的rootViewContr ...

  7. JQUERY操作JSON数组添加新的属性和值

    语法: var data = {}; data["Order"] =order; data["Sort"] = sort; 但是需要注意的是,如果data后面还 ...

  8. UVA Live Archive 4015 Cave (树形dp,分组背包)

    和Heroes Of Might And Magic 相似,题目的询问是dp的一个副产物. 距离是不好表示成状态的,但是可以换一个角度想,如果知道了从一个点向子树走k个结点的最短距离, 那么就可以回答 ...

  9. 动态规划专题(四)——单调队列优化DP

    前言 单调队列优化\(DP\)应该还算是比较简单容易理解的吧,像它的升级版斜率优化\(DP\)就显得复杂了许多. 基本式子 单调队列优化\(DP\)的一般式子其实也非常简单: \[f_i=max_{j ...

  10. z-index、absolute、marquee滚动条的问题

    1.z-index 层次叠加 ,元素叠加,谁的权重大谁就在上面 1).父级出现position:relation:的时候,失效: 2).层叠元素出现float的时候失效: 3).层次元素也得设置pos ...