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 ...
随机推荐
- Cookie简单实例
Cookie简单实例 1.创建CookieServlet package com.servlet.study; import java.io.IOException; import java.io.P ...
- 《DSP using MATLAB》示例Example7.6 Type-3 Linear-Phase FIR
代码: h = [-4, 1, -1, -2, 5, 0, -5, 2, 1, -1, 4]; M = length(h); n = 0:M-1; [Hr, w, c, L] = Hr_Type3(h ...
- flask第十四篇——重定向
我们都知道京东的url是www.jd.com,但是当你输入www.jingdong.com时候,你会发现地址自动跳转到了www.jd.com,这种技术手段就叫做重定向. 重定向分为永久重定向和临时重定 ...
- c++ template不能有cpp
c++的template只能把生命和定义都放在.h文件里,不然会出错
- 使用npm install报错- operation not permitted解决
原文:https://blog.csdn.net/weixin_41715295/article/details/79508104 这几天使用npm install时一直报错-4048 operati ...
- python模块--logging
一.logging模块的简单应用 import logging logging.debug('debug message') logging.info('ingo message') logging. ...
- tomcat nginx 证书切换
1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...
- CRC全套~~~ 转载
经测试CRC16-CCITT是可以了,其它暂时没有测试哦. 00 0E 00 01 00 01 20 17 12 26 20 19 16 01 00 00 01 01 00 00 00 00 00 0 ...
- Spring Boot和Spring cloud
微服务框架SpringBoot简单验证 首先摘录部分IBM网站部分内容对框架做一个简单说明 http://www.ibm.com/developerworks/cn/java/j-lo-spring- ...
- Brocade FC Switch 光信号强度查看
步骤: 1.先运行,porterrshow 查看口状态,然后'statsclear' and 'slotstatsclear' 2.查看:porterrshow,看下port 口的情况 3.收集sup ...