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. Java中String.valueOf、toString、(String)的区别

    原文地址http://blog.csdn.net/yangzhaomuma/article/details/51173138 原文地址https://www.cnblogs.com/xhyouyou/ ...

  2. Django 分页器 缓存 信号 序列化

    阅读目录 分页器 缓存 信号 序列化 Django分页器  (paginator) 导入 from django.core.paginator import Paginator, EmptyPage, ...

  3. beego——URL构建

    如果可以匹配URl,那么beego也可以生成URL吗?当然可以. UrlFor()函数就是用于构建执行函数的URL的.它把对应控制器和函数名结合的字符串作为第一个参数,其余参数对应URL中的变量.未知 ...

  4. 微信小程序组件button

    表单组件button:官方文档 Demo Code: var types=['default', 'primary', 'warn']; var pageObject = { data: { defa ...

  5. springmvc 自定义拦截器

    <mvc:interceptors> <!-- 配置自定义的拦截器 --> <bean class="com.atguigu.springmvc.interce ...

  6. Kattis - cokolada【水】

    Kattis - cokolada[水] 题意 有一个人想吃巧克力,但是巧克力都是按照 2 的幂次的数量包装的,然后他想吃一定数量块的巧克力,然后可以敲碎,每次敲碎都分成两半,比如四块装的分成两块就是 ...

  7. SweetWeather wpf天气预报源代码

    数据来源于中国天气网(www.weather.com.cn)和天气预报网(www.tianqiyubao.com) 主要是对中国天气网和天气预报网接口数据做解析.结合MVVM框架实现. 主界面:   ...

  8. CSS3飘带状3D菜单

    在线演示 本地下载

  9. zabbix api

    #!/usr/bin/env python # -*-coding:utf-8 -*- import requests import json class AutoZabbix: def __init ...

  10. JSONObject使用方法

    转载:http://blog.csdn.net/dongzhouzhou/article/details/8664569 1.JSONObject介绍 JSONObject-lib包是一个beans, ...