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.

感觉不太像medium的题目,但是我写的比价乱啊,代码如下:

 class Solution {
public:
int findPeakElement(vector<int>& nums) {
if(nums.size() == || nums.size() == )
return ;
if(nums[] > nums[])
return ;
for(int i = ; i < nums.size(); ++i){
if(nums[i] > nums[i - ]){
if(i + >= nums.size())
return i;
else if(nums[i] > nums[i + ])
return i;
else ;
}
}
}
};

java代码:

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

LeetCode OJ:Find Peak Element(寻找峰值元素)的更多相关文章

  1. [LeetCode] 162. Find Peak Element 查找峰值元素

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

  2. 162 Find Peak Element 寻找峰值

    峰值元素是指其值大于左右相邻值的元素.给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引.数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以.你可以 ...

  3. lintcode : find peak element 寻找峰值

    题目 寻找峰值 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足 ...

  4. Leetcode162. Find Peak Element寻找峰值

    示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2:   或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...

  5. Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)

    Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...

  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 【 Find Peak Element 】python 实现

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

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

  9. 【leetcode】Find Peak Element ☆

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

随机推荐

  1. hibernate detached分离查询 与 抓取策略注意事项

    1.detached在抓取策略为 jion显式左外连接查询情况下 会产生笛卡儿积现象 DetachedCriteria dc = DetachedCriteria.forClass(Topic.cla ...

  2. 收藏:几种开源许可证的区别!——By 阮一峰制作

    乌克兰程序员Paul Bagwell,画了一张分析图,下面是阮一峰制作的中文版,非常棒,绝对的好东西,收藏这张图供日后查看:

  3. 修改sql server实例、数据库、表、字段的排序规则

    转自:http://blog.51cto.com/jimshu/1095780 概念与详情请参考:字符编码与排序规则:https://www.cnblogs.com/gered/p/9145123.h ...

  4. Linux运维工程师:30道面试题整理

    1.linux 如何挂在 windows 下的共享目录 mount.cifs //192.168.1.3/server /mnt/server -o user=administrator,pass=1 ...

  5. 0503-Hystrix保护应用-feign的hystrix支持

    一.概述 1.1.基础[示例一] 如果Hystrix在类路径上并且feign.hystrix.enabled = true,Feign将用断路器包装所有方法.还可以返回com.netflix.hyst ...

  6. 从0开始用python实现神经网络 IMPLEMENTING A NEURAL NETWORK FROM SCRATCH IN PYTHON – AN INTRODUCTION

    code地址:https://github.com/dennybritz/nn-from-scratch 文章地址:http://www.wildml.com/2015/09/implementing ...

  7. 快速搭建vue脚手架

    https://segmentfault.com/a/1190000011275993

  8. Python集合方法整理(Day9)

    #作用:去重,关系运算, #定义: 知识点回顾 可变类型是不可hash类型 不可变类型是可hash类型 #定义集合: 集合:可以包含多个元素,用逗号分割, 集合的元素遵循三个原则: 1:每个元素必须是 ...

  9. Php DOMDocument 中的 formatOutput

    Nicely formats output with indentation and extra space 是否处理 缩进和多余的空白符

  10. python继承,判断类型,多态

    1.python中继承 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init_ ...