leetcode162
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的更多相关文章
- [Swift]LeetCode162. 寻找峰值 | Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- (leetcode162)find peak element
1题目 A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- LeetCode162.寻找峰值
162.寻找峰值 描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引. 数组可能包含多个峰值,在这种情况下 ...
- leetcode-162周赛-1255-得分最高的单词集合
题目描述: 方法:穷举暴力 class Solution: def maxScoreWords(self, words: List[str], letters: List[str], score: L ...
- leetcode-162周赛-1254-统计封闭岛屿数量
题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r ...
- leetcode-162周赛-1253-重构二进制矩阵
题目描述: 自己的提交: class Solution: def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) ...
- leetcode-162周赛-1252-奇数值单元格数目
题目描述: 自己的提交: class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: ...
- Leetcode162. Find Peak Element寻找峰值
示例 2: 输入: nums = [1,2,1,3,5,6,4] 输出: 1 或 5 解释: 你的函数可以返回索引 1,其峰值元素为 2: 或者返回索引 5, 其峰值元素为 6. 说明: 你的解法 ...
- 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 ...
随机推荐
- Netty系列之一开始使用
Netty是用来做什么的呢,我的理解是它是一个网络开发框架,利用它能很快速方便的开发出高性能的服务端和客户端.刚开始学习java的时候你一定接触过怎么利用socket去实现服务端和客户端,后来java ...
- 写一个Python的windows服务
1. 安装pywin32和pyinstaller pip install pywin32 pip install pyinstaller 2.写一个服务Demo # -*- coding: utf-8 ...
- BZOJ4832: [Lydsy1704月赛]抵制克苏恩(记忆化&期望)
Description 小Q同学现在沉迷炉石传说不能自拔.他发现一张名为克苏恩的牌很不公平.如果你不玩炉石传说,不必担心,小Q 同学会告诉你所有相关的细节.炉石传说是这样的一个游戏,每个玩家拥有一个 ...
- SpringMVC和Freemarker整合,带自定义标签的使用方法
SpringMVC和Freemarker整合,带自定义标签的使用方法. [参考来源:http://www.360doc.com/content/14/1225/14/1007797_435663342 ...
- Uoj 73 未来程序
Uoj 73 未来程序 神仙提答. Subtask 1 仔细阅读,发现是要计算 \(a*b\ \%\ c\).用龟速乘或者 \(python\) 直接算. Subtask 2 仔细阅读并手算一下,发现 ...
- 加密shell
shc -v -r -T -f script-name >/dev/null 2>&1
- CALayer4-自定义层
一.自定义层的方法1 方法描述:创建一个CALayer的子类,然后覆盖drawInContext:方法,使用Quartz2D API进行绘图 1.创建一个CALayer的子类 2.在.m文件中覆盖dr ...
- linux压缩打包等
删除 rm -rf 目录 tar -zcvf /home/xahot.tar.gz /xahot tar -zcvf 打包后生成的文件名全路径 要打包的目录 例子:把/xahot文件夹打包后生成一个/ ...
- C#飞行棋游戏
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- LeetCode 819. Most Common Word
原题链接在这里:https://leetcode.com/problems/most-common-word/description/ 题目: Given a paragraph and a list ...