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] ≠ 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.
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的更多相关文章
- ✡ 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 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 ...
- LeetCode 162 Find Peak Element
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- 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(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- LeetCode——162. Find Peak Element
一.题目链接: https://leetcode.com/problems/find-peak-element/ 二.题目大意: 给定一个长度为N的一维数组,数组是无序的,要求找到数组中的极大值(或局 ...
- 【LeetCode】162. Find Peak Element 解题报告(Python)
[LeetCode]162. Find Peak Element 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/ ...
随机推荐
- shell--题目
1.有一个文件,里面有二列,第一列ip地址,第二列是时间,同一个ip可能出现多次,但时间不同. 文件类似下面的样子: 192.168.1.2 13:10 192.127.12 ...
- hdu 1576 求逆元
题意:给出n=A mod 9973和B,求(A/B) mod 9973 昨天用扩展欧几里得做过这题,其实用逆元也可以做. 逆元的定义:例如a*b≡1 (mod m),则b就是a关于m的逆元. 求逆元方 ...
- Ext comboBox的remote和local的区别
remote模式下不能使用模糊查询的功能 而local模式下可以实现模糊查询的功能 如果非要实现模糊查询的功能,最好就是提前把数据查询出来,缓存到本地,然后再用local模式 且,改个属性,改成可编辑 ...
- WWDC2014总结---For产品经理们
一年一度的苹果开发者大会WWDC2014,在北京时间6月3日凌晨1点开始了,苹果发布了iOS8.OSX10.10等,苹果比以前更加开放了,网上东西很多很杂,但缺少从产品开发角度来梳理的文章. 我根据这 ...
- ppa安装php版本
如果你想安装PHP的特定版本,那么这篇文章可以帮助你.这篇文章将帮助您安装PHP 5.4和PHP 5.5 PHP 5.6,通过使用PPA在Ubuntu 15.10 LTS,14.04或12.04 LT ...
- CSS3 动画animation
关键帧 什么是关键帧.一如上面对Flash原理的描述一样,我们知道动画其实由许多静态画面组成,第一个这样的静态画面可以表述为一帧.其中关键帧是在动画过程中体现了物理明显变化的那些帧. 比如之前的例子中 ...
- ASP.NET MVC 站点设置.html 为起始页
1. 删除 controller="XX" 2. 确保你的工程根目录下的*.htm或*.html文件名在IIS默认文档中存在 搞定
- [Winform]一个简单的账户管理工具
最近一直觉得注册的账户越来越多,帐号密码神马的容易弄混.自己就折腾了一个简单的账户管理工具,其实实现也挺简单,将每个账户的密码及相关密码提示信息,经aes算法加密之后保存到数据库,当前登录用户可以查询 ...
- linux的free命令
free 查看内存使用情况,默认以kb为单位 Mem: total=used+free, 其中buffers和cached是已经使用的内存, 对程序的buffers和cached的理解: os 在内存 ...
- 《C++代码设计与重用》 书评
作者:唐风 主页:www.cnblogs.com/liyiwen 前几个星期买了,一直没有直接细翻,买的时候看了背面的两个推荐,一个是孟岩,一个是Scott Meyers(Effective C+ ...