Python 寻找局部最高点的实现
作者:randomparty 时间:2021-05-10 14:44:00
我就废话不多说了,直接上代码吧!
# 寻找局部最高点
# 输入input: 含有最高点高度的列表
# 输出output: 返回最高点的位置
# 时间复杂度: O(log(n))
def findHighSpot(input):
iLen = len(input)
mid = iLen//2 - 1 # Python中'/'为小数除法,'//'为地板除
if input[mid-1] <= input[mid] >= input[mid+1]:
return str(mid+1) # 默认返回最高点到第一个点之间的距离
elif input[mid-1] > input[mid]:
return findHighSpot(input[:mid//2])
else:
return 'mid+'+findHighSpot(input[mid//2:]) # 返回最高点距离中间点的距离
if __name__ == '__main__':
input = [1, 2, 6, 5, 3, 7, 4]
output = findHighSpot(input)
print('output:', output) # output: 3
来源:https://blog.csdn.net/RandomParty/article/details/80212903
标签:Python,局部,最高点


猜你喜欢
python实操练习案例(一)
2022-01-18 12:10:41

Python如何实现的二分查找算法
2021-10-22 18:39:30
python安装cxOracle避坑总结不要直接pip install
2023-12-07 09:13:11
Javascript removeChild()删除节点及删除子节点的方法
2023-07-02 05:30:22

理解Proxy及使用Proxy实现vue数据双向绑定操作
2024-04-26 17:41:43

tensorflow使用神经网络实现mnist分类
2023-07-05 10:19:13
在VScode中引用自定义模块问题
2023-08-13 03:50:37

浅析Python中MySQLdb的事务处理功能
2024-01-14 01:23:10
PHP封装的数据库模型Model类完整示例【基于PDO】
2023-11-15 21:06:42
解决golang编译提示dial tcp 172.217.160.113:443: connectex: A connection attempt failed(推荐)
2023-07-16 04:24:49
mysql 通配符(sql 高级过滤)
2024-01-24 17:15:39
解决keras backend 越跑越慢问题
2022-05-27 17:36:58
python实现基于两张图片生成圆角图标效果的方法
2023-04-20 17:58:56
python实现多张图片拼接成大图
2021-11-19 08:36:53

GO语言运行环境下载、安装、配置图文教程
2024-05-11 09:09:11

MySQL中利用索引对数据进行排序的基础教程
2024-01-21 16:04:10
PyQt Qt Designer工具的布局管理详解
2023-09-21 04:44:17

Python计算字符宽度的方法
2021-02-13 20:25:28
新手如何发布Python项目开源包过程详解
2023-02-27 13:08:05

PHP入门教程之会话控制技巧(cookie与session)
2023-11-16 00:13:39