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. .NET实现CORS跨域

    1.NuGet:Install-Package Microsoft.AspNet.WebApi.Cors 2.Application_Start中增加(包含在#if DEBUG中):GlobalCon ...

  2. BZOJ3325 [Scoi2013]密码【Manacher】【构造】【贪心】

    Description Fish是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进.通过翻阅古籍,Fish 得知了这个密码的相关信 ...

  3. 将 async/await 异步代码转换为安全的不会死锁的同步代码

    在 async/await 异步模型(即 TAP Task-based Asynchronous Pattern)出现以前,有大量的同步代码存在于代码库中,以至于这些代码全部迁移到 async/awa ...

  4. pat甲级 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  5. selenium数据驱动

    Selenium最后一个知识点——数据驱动.学会了这个Selenium就算学完啦~ 看代码: 这是修改的testSell.py文件. # coding: utf-8 import ddtimport ...

  6. nginx ngscript 简单使用

    备注: 默认没有集成到nginx包里,需要单独安装(推荐使用动态模块的方式进行安装) 1. 安装 wget https://nginx.org/download/nginx-1.13.11.tar.g ...

  7. 当Ucenter和应用通信失败,DZ数据库备份恢复

    http://blog.sina.com.cn/s/blog_775f158f010135uz.html ucenter整合自己的项目 http://jingyan.baidu.com/article ...

  8. Jacoco在eclipse上的集成使用

    随着敏捷开发的流行,编写单元测试已经成为业界共识.但如何来衡量单元测试的质量呢?有些管理者片面追求单元测试的数量,导致底下的开发人员投机取巧,编写出大量的重复测试,数量上去了,质量却依然原地踏步.相比 ...

  9. Fuel9.0安装openstack过程中所踩过的坑2018最新版

    坑一,安装好后,无法访问Web UI画面 访问https//10.20.0.2:8443无法打开UI画面.首先我们不管以后的步骤,打不开是很不爽的. 解决方法:把下面网卡1,网卡2,网卡3的界面名称都 ...

  10. SharePoint2010基于表单验证方法总结(转载)

    系统环境: win2008r2+ sql2008r2 +Visual Studio2010+sharepoint 2010 A.如果已经建立了web application  例如名字为: http: ...