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. centos7 重启网卡失败

    今天在centOS 7 network服务重启不了 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行service network restart命令后出现下面的错误: ...

  2. fopen() r+、w+属性详解

    r+具有读写属性,从文件头开始写,保留原文件中没有被覆盖的内容: w+具有读写属性,写的时候如果文件存在,会被清空,从头开始写. r 打开只读文件,该文件必须存在. r+ 打开可读写的文件,该文件必须 ...

  3. [转载]有经验的Java开发者和架构师容易犯的10个错误

    首先允许我们问一个严肃的问题?为什么Java初学者能够方便的从网上找到相对应的开发建议呢?每当我去网上搜索想要的建议的时候,我总是能发现一大堆是关于基本入门的教程.书籍以及资源.同样也发现网上到处充斥 ...

  4. CAS单点登录实践(spring cas client配置)

    前言: 最近的项目需要将多个站点统一登录,查阅了资料Jasig cas(Central Authentication Service)(官方站点:http://www.jasig.org/cas)使用 ...

  5. AVL(二叉平衡树) 的实现

    一颗AVL树是其每个节点的左子树与右子树的高度最多差1的二叉查找树. 在插入过程中,利用旋转的办法保持这个性质. 共分四种情形: 1.  树T的左孩子的左子树上新插入节点导致破坏平衡性: 如下图左边所 ...

  6. Delphi 正则表达式语法(4): 常用转义字符与 .

    Delphi 正则表达式语法(4): 常用转义字符与 . // \d 匹配所有数字, 相当于 [0-9] var   reg: TPerlRegEx; begin   reg := TPerlRegE ...

  7. s5_day1作业

    #1.使用while循环输出1 2 3 4 5 6 8 9 10 # s=0 # while s<10: # s+=1 # if s==7: # continue # print(s) # fo ...

  8. mysql第二天作业

    create database 数据库名 default charset utf8;use 数据库名;1.创建成绩表,字段包括:学生姓名,语文成绩,数学成绩,英语成绩create table resu ...

  9. [翻译]Feedback on the Go Challenge solutions

    第一次Go Challenge比赛,中国区只有3人参赛. 赛后收到邮件,是一个审阅者的反馈,“Feedback on the Go Challenge solutions”,摘录如下: 保持简单粗暴 ...

  10. springmvc get post put delete

    web.xml <!-- 配置 org.springframework.web.filter.HiddenHttpMethodFilter: 可以把 POST 请求转为 DELETE 或 POS ...