leetcode 日记 162. Find Peak Element java python

根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号。并且要求时间复杂度为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的更多相关文章
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
- ✡ 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] ≠ ...
- 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] ≠ ...
- 【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 ...
- 【刷题-LeetCode】162 Find Peak Element
Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...
- 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] ≠ ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- (二分查找 拓展) 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 ...
随机推荐
- laravel安装学习步骤
在看知乎比较php框架的优劣的时候提到为什么laravel这么好国内用的少,还有就是yii2,有人提到原因就是composer在国内无法使用.这制约了使用composer进行包管理的框架在国内的传播和 ...
- Java准确地获取本地IP地址
问题 用Java获取本机IP地址,需要处理: 1. 多块网卡. 2. 排除loopback设备.虚拟网卡 看似简单的代码,写起来还是要小心一些的. 方案 HBase客户端获取本机IP的代码提供了一个很 ...
- goroutine
Go语言从诞生到普及已经三年了,先行者大都是Web开发的背景,也有了一些普及型的书籍,可系统开发背景的人在学习这些书籍的时候,总有语焉不详的感觉,网上也有若干流传甚广的文章,可其中或多或少总有些与事实 ...
- 28. Implement strStr()
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- nginx.conf 解释
http://snapshot.sogoucdn.com/websnapshot?ie=utf8&url=http%3A%2F%2Fwww.cszhi.com%2F20120513%2Fngi ...
- js基本类型 引用类型
参考 https://segmentfault.com/a/1190000005794070 http://blog.csdn.net/yummy_go/article/details/5050468 ...
- DOM hash
前段时间做的一个H5专题,用到了hash解决问题,特意记录一下.DOM hash的详细内容可以点击链接查看. hash就是uri中#及后面的部分,例如:www.google.com.hk#123的#1 ...
- mybatis if test 不为空字符串或null
<if test="type !=null and type !=''"> AND l.type=#{type,jdbcType=INTEGER} </if> ...
- kaggle数据挖掘竞赛初步--Titanic<随机森林&特征重要性>
完整代码: https://github.com/cindycindyhi/kaggle-Titanic 特征工程系列: Titanic系列之原始数据分析和数据处理 Titanic系列之数据变换 Ti ...
- 从客户端(txtContent="<p>1</p>")中检测到有潜在危险的 Request.Form 值
输入1也报这个错误, <pages validateRequest="false" 改了也不行,在页头改也不行.到底什么情况呢? 从这个地方找到了答案:http://nt.d ...