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 ...
随机推荐
- About vector
今天打vector又打炸了不!高!兴! vecotr头文件 #include<vector> 定义域 using namespace std; 或using std::vector; 初始 ...
- PostGIS导入导出SHP文件常用命令
SHP导入POSTGIS数据库 引用 直接导入数据库 shp2pgsql -I -s 2437 -W GBK shop_point.shp public.ntable | psql -U postg ...
- 学习UFT11.5历程(三)
已经用UFT11.5完成了几个大流程的录制和脚本调测. 现整理下这段过程中脚本中应该记住的点(QTP是VB脚本): 1. 循环和条件部分_reporter结果展示 For i = 1 To brow ...
- tcpdump用法
http://man.linuxde.net/tcpdump http://www.cnblogs.com/yc_sunniwell/archive/2010/07/05/1771563.html
- MVC使用x.PagedList分页
MVC分页 1.Install Package Tools=> NuGet Package Manager=>Manager NuGet Packages of Solution
- JDBC与Hibernate中SQL语句参数设置的顺序问题
JDBC中:设置从1开始 例: Connection con = DriverManager.getConnection("jdbc:mysql://localhost/...", ...
- Gvim常用命令
这是一篇较全的vim命令.特记录下来,有稍作修改说明.摘http://hi.baidu.com/ui176/item/b00ae7c0eeaba52847d5c0c5 Vim常用命令 跳到指定的行号: ...
- could not read symbols: File format not recognized
arm-linux-gnueabi-readelf工具解决问题 编译一个32位平台的内核时,出现如下错误提示: libschw.a: could not read symbols: File form ...
- 求子串-KPM模式匹配-NFA/DFA
求子串 数据结构中对串的5种最小操作子集:串赋值,串比较,求串长,串连接,求子串,其他操作均可在该子集上实现 数据结构中串的模式匹配 KPM模式匹配算法 基本的模式匹配算法 //求字串subStrin ...
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...