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

补充一个python的实现,使用二分查找:

 class Solution:
def findPeakElement(self, nums: List[int]) -> int:
l, r = , len(nums) - ;
while l < r:
m = l + (r - l) //
if nums[m] > nums[m + ]:
r = m
elif nums[m] < nums[m + ]:
l = m +
return l

leetcode162的更多相关文章

  1. [Swift]LeetCode162. 寻找峰值 | Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

  2. (leetcode162)find peak element

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

  3. LeetCode162.寻找峰值

    162.寻找峰值 描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下 ...

  4. leetcode-162周赛-1255-得分最高的单词集合

    题目描述: 方法:穷举暴力 class Solution: def maxScoreWords(self, words: List[str], letters: List[str], score: L ...

  5. leetcode-162周赛-1254-统计封闭岛屿数量

    题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r ...

  6. leetcode-162周赛-1253-重构二进制矩阵

    题目描述: 自己的提交: class Solution: def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) ...

  7. leetcode-162周赛-1252-奇数值单元格数目

    题目描述: 自己的提交: class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: ...

  8. Leetcode162. Find Peak Element寻找峰值

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

  9. LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element

    二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...

随机推荐

  1. 【剑指offer】09-2跳台阶,C++实现

    原创博文,转载请注明出处! # 本文是牛客网<剑指offer>刷题笔记 1.题目 # 一只青蛙一次可以跳1级台阶,也可以跳2级.求该青蛙跳n级的台阶总共有多少种跳法. 2.思路 # 跳0级 ...

  2. C++ 回调函数的几种策略

    Stackoverflow中提出了这样一个问题:假设我们实现了一个User类,Library类,现在Library类中utility需要回调User中func方法,总结答案,常见的几种方法如下: 静态 ...

  3. Mysql 分组查询最高分

    今天告诉我要写一个服务,目的是按照每个班中各分组中竞赛最高分组平分小组得分给各个成员的服务,于是就有两个技术需求 1 查询每个班的冠军团队 2 增加一组人的分数 从“1”中,查出每个班N个分组中的得分 ...

  4. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  5. Java并发--Timer和TimerTask

    下面内容转载自: http://blog.csdn.net/xieyuooo/article/details/8607220 其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了r ...

  6. 解决遇到Linux网络配置,从熟悉网络配置文件入手

    如果接触过Linux,网络配置是一个比较棘手的问题.但是Linux是文件为基础来构建的系统,包括我们windows中设备,Linux也视为文件.所以只要我们明白文件的作用.就能对Linux更加的熟悉, ...

  7. Thinkphp 下 MySQL group by 接count 获得条数方法

    比如 下面的语句 , 用于分组统计 select count(*) from es_diabetes where uid=43658 GROUP BY uniques 结果明显不是我们想要得,为什么呢 ...

  8. 【深度学习笔记】Anaconda及开发环境搭建

    在学习了一段时间台大李宏毅关于deep learning的课程,以及一些其他机器学习的书之后,终于打算开始动手进行一些实践了. 感觉保完研之后散养状态下,学习效率太低了,于是便想白天学习,晚上对白天学 ...

  9. filter防盗链

    1这次练习中一直受到相对路径的干扰,现在澄清一点 forward中不是不需要包含根目录的  比如 http://localhost:8080/filter/upload/images/no.jpg 你 ...

  10. 搭建一个IntelliJ的Spark项目

    之前发现创建一个新项目之后,无法添加scala class 创建新项目 选择maven项目,然后选择simple或者quickstart: 进入项目后,在Project Structure里面,在gl ...