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. HDU 3265 扫描线(矩形面积并变形)

    Posters Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  2. hdu 4602 Partition

    http://acm.hdu.edu.cn/showproblem.php?pid=4602 输入 n 和 k 首先 f(n)中k的个数 等于 f(n-1) 中 k-1的个数 最终等于 f(n-k+1 ...

  3. Java longTime 和C#日期转换

    封装一下,可直接用. 以后碰到java的long time,直接使用DateTime dt=ConvertJavaDateTimeToNetTime(1207969641193);这样使用即可. 这串 ...

  4. ASP.NET 学习笔记

    1.ASP.NET 服务器控件是可被服务器理解的标签 有三种类型的服务器控件(所有服务器控件必须出现在 <form> 标签内,同时 <form> 标签必须包含 runat=&q ...

  5. hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏

    partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...

  6. 2014年3月份第2周51Aspx源码发布详情

    MVC+EF某钢电子交易平台源码  2014-3-10 [VS2012]功能介绍:本源码是一套完整的电子交易平台系统,完全基于ASP.NET MVC+EF三层构架,开发环境为Visual Studio ...

  7. CGAffineTransformMakeTranslation和CGAffineTransformTranslate

    分类: ios基础2013-01-06 22:05 15513人阅读 评论(2) 收藏 举报 1.CGAffineTransformMakeTranslation每次都是以最初位置的中心点为起始参照 ...

  8. web api post传一个参数时 值永远是null

    这个问题纠结了我一个早上,不管用什么样的传参方法,走到控制器中,那个参数永远不变的等于null 在网上找了很多解决方案 上面这个是从网上截图的,第一:要将参数标记为[FromBody],变为简单参数 ...

  9. Canopy使用教程 (2)

    1.下载https://reputation.alienvault.com/reputation.data alienvault公司的IP信誉数据库文件到本地,手动或者wget 2.使用 read_c ...

  10. Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m

    //  YJYMasterViewController.m #import "YJYMasterViewController.h" #import "YJYDetailV ...