HashSet查找复杂性? [英] HashSet look-up complexity?

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

问题描述

对于单个的查找操作OR 包含,最坏情况下可以 O(n)吗?因此,对于 n 元素在中查找hashSet 将是 O(n ^ 2)

A look-up operation OR contains for single can be O(n) in worst-case right ? So, for n elements look up in hashSet will be O(n^2)?

推荐答案

是的,但这确实是最糟糕的情况:如果<$ c中的所有元素$ c> HashSet 具有相同的哈希码(或通向同一桶的哈希码)。使用正确编写的 hashCode 和正态分布的密钥样本,查找为O(1)。

Yes, but it's really the worst case: if all the elements in the HashSet have the same hash code (or a hash code leading to the same bucket). With a correctly written hashCode and a normally distributed key sample, a lookup is O(1).

这篇关于HashSet查找复杂性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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