Python反向列表的时间复杂度是多少? [英] What is the time complexity of Python List Reverse?
问题描述
我看过此页面 https://wiki.python.org/moin/TimeComplexity,但我看不到列表中的reverse()
函数. list
的reverse()
的时间时间复杂度是多少?
I have seen this page https://wiki.python.org/moin/TimeComplexity but I don't see the reverse()
function in there for lists. What is the time time complexity of list
's reverse()
?
我对时间的实验表明,对于较大的尺寸,它为O(n)
.有人可以确认吗?
My experiments with time indicate that it is O(n)
for larger sizes. Can anyone confirm it ?
timeit反转大小列表的时间
timeit Time to reverse a list of size
10 .1027
100 .2347
1000 .6704
10000 6.204
20000 12.9
推荐答案
是的,您是对的,它是O(n),其中n-列表的长度. 在此处查找更多信息: https://www.ics. uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
Yes, you are right, it is O(n) where n - length of list. Look here for more information: https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
这篇关于Python反向列表的时间复杂度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!