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 String是不可变对象

    基本数据类型和String类型都是值传递,数组,对象等是引用传递 经多方面查找,String很奇特,虽然是引用数据类型,但是采用的却是值传递!!!基本数据类型采用的都是值传递,数组和对象都是引用传递( ...

  2. BZOJ-1879 Bill的挑战 状态压缩DP

    MD....怎么又是状压....... 1879: [Sdoi2009]Bill的挑战 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 537 Solved ...

  3. BZOJ3720 Gty的妹子树

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  4. Codeforces 578B "Or" Game

    传送门 B. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. ubuntu 14.04 vim install youcompleteme

    sudo apt-get install vim ; sudo apt-get install vim-youcompleteme ; sudo apt-get install vim-addon-m ...

  6. Android 实现卫星菜单

    步骤:一:自定义ViewGroup 1.自定义属性 a.attr.xml b.在布局文件中使用activity_main.xml c.在自定义控件中进行读取 2.onMeasure 3.onLayou ...

  7. php返回json数据简单实例

    <?php include './include/conn.php'; //数据库链接文件 $sql_notice = mysql_query('SELECT * FROM gg_notice ...

  8. Nginx 配置文件详解

    user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error.log crit; #错误 ...

  9. 使用servletAPI三种方式简单示例

    一.直接实现Action接口或集成ActionSupport类(推荐) public class HelloAction implements Action { @Override public St ...

  10. The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1)

    The Dirichlet Distribution 狄利克雷分布 (PRML 2.2.1) Dirichlet分布可以看做是分布之上的分布.如何理解这句话,我们可以先举个例子:假设我们有一个骰子,其 ...