有效地找到可变数量的字符串集的交集 [英] Efficiently finding the intersection of a variable number of sets of strings

查看:126
本文介绍了有效地找到可变数量的字符串集的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可变数量的ArrayList,我需要找到它的交集。字符串数量的实际上限可能在35左右,但可能更多。我不想要任何代码,只需要关于什么是有效的想法。我有一个实现,我即将开始编码,但想听听其他一些想法。

I have a variable number of ArrayList's that I need to find the intersection of. A realistic cap on the number of sets of strings is probably around 35 but could be more. I don't want any code, just ideas on what could be efficient. I have an implementation that I'm about to start coding but want to hear some other ideas.

目前,只考虑我的解决方案,看起来我应该有一个Θ的渐近运行时间(n 2 )。

Currently, just thinking about my solution, it looks like I should have an asymptotic run-time of Θ(n2).

感谢您的帮助!

tshred

编辑:为了澄清,我真的只是想知道有更快的方法来做到这一点。比Θ更快(n 2 )。

To clarify, I really just want to know is there a faster way to do it. Faster than Θ(n2).

推荐答案

Set.retainAll() 是你如何找到两组的交集。如果您使用 HashSet ,则将 ArrayList 转换为 Set s并在所有这些循环中使用 retainAll()实际上是O(n)。

Set.retainAll() is how you find the intersection of two sets. If you use HashSet, then converting your ArrayLists to Sets and using retainAll() in a loop over all of them is actually O(n).

这篇关于有效地找到可变数量的字符串集的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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