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.

代码如下:

 public class Solution {
public int findPeakElement(int[] nums) {
if(nums.length==1)
return 0;
if(nums.length==2&&nums[0]<nums[1])
return 1; for(int i=1;i<nums.length-1;i++)
{
if(nums[i]>nums[i-1]&&nums[i]>nums[i+1])
return i;
}
if(nums[nums.length-1]>nums[nums.length-2])
return nums.length-1; return 0;
}
}

162. Find Peak Element的更多相关文章

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

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

  2. LeetCode 162 Find Peak Element

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

  3. 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] ≠ ...

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

  5. 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] ≠ ...

  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 && lintcode 75. Find Peak Element

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

  8. 162. Find Peak Element (Array; Divide-and-Conquer)

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

  9. 162. Find Peak Element(二分查找 )

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

随机推荐

  1. java日期的运用(DateUtils工具类)

    public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sd = new SimpleDate ...

  2. comboBox绑定数据库、模糊查询

    实现: 一.绑定数据库 点击查询按钮,comboBox显示从数据库查到的某字段的一列数据 方法:在按钮的点击事件绑定数据库 private void button1_Click(object send ...

  3. Ibatis.Net 入门示例(一)

    新项目用的是Ibatis.Net.哥的感觉是NHibernate才是哥的最爱.没办法,即使重复,也要抽时间学习.努力做好这份工作再说吧. 一.Ibatis.Net-SQLServer2008 还是以哥 ...

  4. Java 时间、日期类

    1. System类 currentTimeMillis():返回当前时间的long型值.此long值是从1970年1月1日0点0分00秒开始到当前的毫秒数. 此方法常用来计算时间差. 2. Date ...

  5. 【转发】Linux下如何查看当前支持的文件系统及各分区的文件系统类型

    Linux下查看当前内核系统支持的文件系统: 一般都在 /lib/modules/kernl-version/kernel/fs/ 目录下包含了当前内核版本支持的文件系统: ls /lib/modul ...

  6. [Java]eclipse的使用

    1.android sdk help安装 使用SDK Manager.exe下载android sdk的时候把docs也勾选上. 在eclipse的android工程下的android.jar(在an ...

  7. Thread IsBcakgroud

    C#中,Thread类有一个IsBackground 的属性.MSDN上对它的解释是:获取或设置一个值,该值指示某个线程是否为后台线程.个人感觉这样的解释等于没有解释. .Net中的线程,可以分为后台 ...

  8. java基础-004

    ---恢复内容开始--- 14.Java集合类框架的基本接口 集合类接口指定了一组叫做元素的对象.集合类接口的每一种具体的实现类都可以选择以它自己的方式对元素进行保存和排序.有的集合类允许重复的键,有 ...

  9. Nginx 缓存参数

    看看这下面两个指令参数: ----------------------------------------------------------------- proxy_cache_path  /ho ...

  10. WIN中SharePoint Server 2010 入门安装部署详解

    目前流行的原始安装文件基本都是这样的:Windows Server 2008 R2+SQL Server 2008R2+SharePoint Server 2010     这个初始环境原本也无可厚非 ...