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.

Note:

Your solution should be in logarithmic complexity.

解题思路:

题目的提示中要求用log(n)的时间复杂度,因此,只能采用分治的思路,类似归并排序,JAVA实现如下:

    public int findPeakElement(int[] nums) {
if(nums.length==1||nums[0]>nums[1])
return 0;
if(nums[nums.length-1]>nums[nums.length-2])
return nums.length-1;
int left=1,right=nums.length-2,mid=(left+right)/2;
if(findPeakElement(nums,mid,right)==-1)
return findPeakElement(nums,left,mid);
return findPeakElement(nums,mid,right);
}
static public int findPeakElement(int[] nums,int left,int right) {
if(left==right){
if(nums[left]>nums[left-1]&&nums[left]>nums[left+1])
return left;
return -1;
}
else if(left==right-1){
if(nums[left]>nums[left-1]&&nums[left]>nums[left+1])
return left;
else if(nums[right]>nums[right-1]&&nums[right]>nums[right+1])
return right;
return -1;
}
int mid=(left+right)/2;
if(findPeakElement(nums,mid+1,right)==-1)
return findPeakElement(nums,left,mid);
return findPeakElement(nums,mid+1,right);
}

Java for LeetCode 162 Find Peak Element的更多相关文章

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

  2. LeetCode 162. Find Peak Element (找到峰值)

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

  3. [LeetCode] 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 && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  5. LeetCode 162 Find Peak Element

    Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...

  6. 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(M)(P)

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  8. LeetCode——162. Find Peak Element

    一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...

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

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

随机推荐

  1. Java Filter过滤器的简单总结

    1.Filter的介绍 Filter技术是servlet 2.3新增加的功能.它能够对Servlet容器的请求和响应对象进行检查和修改. Filter本身并不生成请求和响应对象,只是提供过滤功能. F ...

  2. druid(德鲁伊)数据源的使用和配置 阿里出品

    pom.xml <dependency>     <groupId>com.alibaba</groupId>     <artifactId>drui ...

  3. 安装最新版本的ReSharper导致原生全局搜索工具的消失问题

    经过检测,是由于启用了一个插件导致的,禁用即可,如下图:

  4. (转)google Java编程风格中文版

    转:http://www.hawstein.com/posts/google-java-style.html 目录 前言 源文件基础 源文件结构 格式 命名约定 编程实践 Javadoc 后记 前言 ...

  5. iOS代码工具箱再续

    if (CGRectContainsPoint(self.menuView.frame, point)) { point =  [self.view convertPoint:point toView ...

  6. Java中反射机制和Class.forName、实例对象.class(属性)、实例对象getClass()的区别

    一.Java的反射机制   每个Java程序执行前都必须经过编译.加载.连接.和初始化这几个阶段,后三个阶段如下图:   其中

  7. 实现JavaScript自定义函数的整合、链式调用及类的封装

    函数声明形式:表单验证函数 1 2 3 4 5 6 7 8 9 10 11 12 13 function checkName(){     console.log('检查用户名'); } functi ...

  8. 论文的构思!姚小白的html5游戏设计开发与构思----给审核我论文的导师看的

    此处只为笔记 游戏么基本上确定是用canvas做个能一只手玩的游戏!基本打飞机之类的.毕竟手机也就上下班玩玩的.上下班么基本就是一只手拉着扶手一只手撸啊撸! 当然啦,如果能搞出超级牛逼的游戏,比如刺客 ...

  9. 微信稳居Android App排行榜4月份国内榜首

    根据某机构通过对Android样本访问行为的持续监测数据进行样本属性加权并根据iOS/Android用户调研数据建模推总得出中国移动互联网用户规模以及相应的用户结构数据显示,2015年4月份国内And ...

  10. 关于Eclipse部署openfire3.8.2源码的体会

    因为公司要做人际银行的一个项目需要openfire(服务器)+asmack(客户端),所以需要对消息的推送及消息发送知识的积累.所以需要研究xmpp,以前不是很了解这个技术,现在需要学习.首先就得部署 ...