Python 寻找局部最高点的实现

yipeiwu_com7年前Python基础

我就废话不多说了,直接上代码吧!

# 寻找局部最高点
# 输入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

以上这篇Python 寻找局部最高点的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法

可能很多人遇到过这个错误,当使用setup.py安装python2.7图像处理模块PIL时,python默认会寻找电脑上以安装的vs2008.如果你没有安装vs2008,会出现Unabl...

Python优化技巧之利用ctypes提高执行速度

首先给大家分享一个个人在使用python的ctypes调用c库的时候遇到的一个小坑 这次出问题的地方是一个C函数,返回值是malloc生成的字符串地址。平常使用也没问题,也用了有段时间,...

python:接口间数据传递与调用方法

如下所示: import requests import unittest import json from pubulic_way.get_token import getSess...

详谈Python2.6和Python3.0中对除法操作的异同

Python中除法有两种运算符:'/'和'//';有三种类型的除法:传统除法、Floor除法和真除法。 X / Y类型: 在Python2.6或者之前,这个操作对于整数运算会省去小数部分...

python随机生成库faker库api实例详解

废话不多说,直接上代码! # -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/6/30 9:49 from fake...