什么是设置转换的列表的时间复杂度? [英] What is time complexity of a list to set conversion?

查看:136
本文介绍了什么是设置转换的列表的时间复杂度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到python官方网站上设置操作的时间复杂度表。但我只想问一下将列表转换为集合的时间复杂度,例如,

I've noticed the table of the time complexity of set operations on the python official website. But i just wanna ask what's the time complexity of converting a list to a set, for instance,

l = [1, 2, 3, 4, 5]
s = set(l)

我知道这实际上是一个哈希表,但它究竟是如何工作的?那么它是O(n)吗?

I kind of know that this is actually a hash table, but how exactly does it work? Is it O(n) then?

推荐答案

是的。在列表中迭代 O(n),并且将每个元素添加到散列集 O(1),那么总操作是 O(n)

Yes. Iterating over a list is O(n) and adding each element to the hash set is O(1), so the total operation is O(n).

这篇关于什么是设置转换的列表的时间复杂度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆