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

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

问题描述

我在python官网上看到了set操作的时间复杂度表.但我只想问问将列表转换为集合的时间复杂度是多少,例如,

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天全站免登陆