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. android webview 漏洞背后的节操

    by superhei 2013/09/06 [注:本文提到的都是我个人的观点,该行为也是私人行为,与任何组织.公司无关.另:水军请自重!] 一.前言   这两天,一个2+年前的android web ...

  2. c++ 普通高精乘

    //第一次提交全错了,太过相信自己了. //给我教训是注意循环后变量的值,和pascal不一样. //就不贴错误代码了. //codevs3118 高精度练习之除法 #include<cstdi ...

  3. Operating System Concepts 项目:生产者-消费者问题 线程

    一. 实验目的 实现一个c程序,该程序能模拟解决有限缓冲问题,其中消费者和生产者产生和消耗随机数 二.实验内容 缓冲区 元数据类型为buffer_item,大小为1000的数组,按环形队列处理 生产者 ...

  4. COleDateTime类型的应用

    使用COleDateTime类1) 获取当前时间.      CTime time;      time = CTime::GetCurrentTime();2) 获取时间元素.      int y ...

  5. Hello Hibernate

    Hibernate 一个框架; 一个 Java 领域的持久化框架; 一个 ORM 框架 ORM(Object/Relation Mapping): 对象/关系映射 –ORM的思想:将关系数据库中表中的 ...

  6. Cisco IOS Software Activation Command Reference

    clear license agent : to clear license agent statistics counters or connection statistics (in privil ...

  7. 获取UIColor中的RGB值(本人亲测多个获取RGB值的方法,这个最有效)

    在自己研发的项目个人项目中,碰到一个从颜色中获取RGB值的需求. 在网上找了许久,也有一些方法可以获取RGB值,但不能获取黑白以及灰色的值(他们是非RGB颜色空间,不清楚什么意思,反正亲测确实获取不了 ...

  8. hdu1506 dp

    //Accepted 1428 KB 62 ms // #include <cstdio> #include <cstring> #include <iostream&g ...

  9. 关于netbeans中的JComboBox(复选框)

    以最近写的选课系统中添加课程项为例 1.往复选框中放入选项(根据数据库添加) (1)首先将  属性—>model中默认Item1234清空 (2)获得数据库中的数据并放入. SelectCour ...

  10. ajax注释

    //xmlHttpRequest,但是这个对象只是在火狐,google...//在中国用的最广泛的IE浏览器里面是没有这个对象的//在IE里面是用的一个控件来解决这个问题,ActiveXObject/ ...