从列表构建集合的复杂性? [英] Complexity of building a set from list?

查看:30
本文介绍了从列表构建集合的复杂性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下操作的计算复杂度是多少:

What is the computational complexity of the following operation:

list1 = [1,2,2,2,3,4,4]
set1 = set(list)

推荐答案

它是 O(n),其中 n 是列表中元素的数量.set() 本质上是遍历列表并将这些元素中的每一个添加到哈希表中.

It is O(n), where n is the number of elements in the list. set() is essentially iterating over the list and adding each of those elements to a hash table.

这篇关于从列表构建集合的复杂性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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