繁体   English   中英

关于“ in” Python时间复杂度的简单查询

[英]Simple query about time complexity for Python “in”

我有一个函数strip_punctuation(text),它接收一个文本字符串,并使用标点符号列表删除其中的所有标点符号。 我不确定时间复杂度是O(N)* N还是O(N ^ 2)。 我认为这适用于N个文本长度为O(N),然后为标点符号长度为O(N)的情况。 有人可以澄清这段代码的时间复杂度吗?

def strip_punctuation(text):
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    stripped = ""
    for i in text:
        if i not in punctuations:
            stripped = stripped + i

    return stripped

如果N为len(text) ,则为O(N):

for i in text

如果M为len(punctuations) ,则此代码为O(M ^ 2):

if i not in punctuations:
    stripped = stripped + i

这是因为整个stripped (长度> = M)必须被复制M次( stripped + i构成stripped的副本)。

因此,如果同时输入了textpunctuations ,则复杂度为O(N)* O(M ^ 2),但是在这种情况下,M为常数,因此复杂度为O(N)

请注意,如果punctuations非常大,该函数将非常慢,但其复杂度仍仅为O(N),这仅意味着当输入大N倍时,它的速度慢N倍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM