题目
描述
给定一个长度为n的数组nums,请你找到峰值并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个所在位置即可。
思路
只要某个峰比旁边两个峰高就可以了,找出这个峰的索引就行。
代码
python版本:
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @return int整型
#
class Solution:
def findPeakElement(self , nums: List[int]) -> int:
# write code here
nums.append(0)
nums.insert(0, 0)
length = len(nums)
res = 0
for i in range(1, length-1):
if nums[i-1] < nums[i] and nums[i+1]<nums[i]:
res = i-1
break
return res
c++版本:
无