当前位置:网站首页>LeetCode 162.寻找峰值(中等)
LeetCode 162.寻找峰值(中等)
2022-08-10 05:32:00 【生信研究猿】
python
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
if(len(nums)==1):
return 0
if(nums[0]>nums[1]):
return 0
if(nums[len(nums)-1]>nums[len(nums)-2]):
return len(nums)-1
'''
复杂度O(n)
i = 1
while(1):
if(nums[i]>nums[i-1] and nums[i]>nums[i+1]):
break
i +=1
return i
'''
#二分法
left = 0
right = len(nums)-1
while(left<right):
mid = int((left + right )/2)
if(nums[mid]>nums[mid+1]):
right = mid
else:
left = mid +1
return left
边栏推荐
猜你喜欢
随机推荐
LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
最新最全的数字藏品发售日历-07.26
wiki confluence installation
Day1 微信小程序-小程序代码的构成
Multi-table query Notes
transaction, storage engine
第二次实验
LeetCode 面试题17.14 最小k个数(中等)
redis常见的面试题
generic notes()()()
国内数字藏品投资价值分析
Link reading good article: What is the difference between hot encrypted storage and cold encrypted storage?
网络安全3
链表API设计
反射【笔记】
图片批量添加水印批量缩放图片到指定大小
Bifrost micro synchronous database implementation services across the library data synchronization
Chain Reading Good Article: Jeff Garzik Launches Web3 Production Company
Collection set interface
Reprint fstream, detailed usage of ifstream