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 ...
随机推荐
- js做计算器
js文档: function count(){ var a var txt1=document.getElementById("txt1").value;//获取第一个输入框的值 ...
- Gvim常用命令
这是一篇较全的vim命令.特记录下来,有稍作修改说明.摘http://hi.baidu.com/ui176/item/b00ae7c0eeaba52847d5c0c5 Vim常用命令 跳到指定的行号: ...
- 关于UIView的AutoresizingMask属性的研究
在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum ...
- MySQL数据库常用命令
1.连接mysql数据库:mysql -u用户名 -p密码; 2.创建数据库:create database 数据库名称; 3.删除数据库:drop database 数据库名称; 4.使用数据库:u ...
- JAVA04类与对象之课后问题解决
1.验证ClassAndObjectTest.java(使用自定义类) public class ClassAndObjectTest { public static void main(String ...
- (转抄:人人都是产品经理——iamsujie)如何提高产品规划PPT的能力
做产品几年之后,不可避免的要碰到写规划这件事儿,虽说不少人,不乏大牛,对规划持“无用论”的观点, 但大多数人其实还是更相信这个段子: 在一个公司里,看一个人的地位,主要看他平时写的文档类型——写wor ...
- java 选择排序
import java.util.Scanner; public class SelectionSort { public static void sort(int[] a, int n){ if(n ...
- discuz手机版模板开发
1.触屏版模板手机路径 discuz X3触屏版模板路径:/template/default/touch/forum/discuz.htm(主页面模板) discuz X3标准版模板路径:/templ ...
- iOS 运行时runtime控制私有变量以及私有方法
OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtime机制让我们可以在程序运行时动态修改类.对象中的所有属性.方法,就算是私有方法以及私有属性都是可以动 ...
- HTML5 和HTML4的区别
1.推出理由和目标 HTml5的出现,对于web来说意义是非常重大的,因为它的意图是想要把目前web 上存在的各种问题一并解决掉. (1)web之间的兼容性很低 (2)文档结构不明确 (3)web应用 ...