高效的列表交集算法 [英] Efficient list intersection algorithm

查看:24
本文介绍了高效的列表交集算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个列表(不一定要排序),找到这些列表的集合交集的最有效的非递归算法是什么?
我不相信我可以使用哈希算法.

Given two lists (not necessarily sorted), what is the most efficient non-recursive algorithm to find the set intersection of those lists?
I don't believe I have access to hashing algorithms.

推荐答案

您可以将第一个列表的所有元素放入一个哈希集中.然后,迭代第二个,对于它的每个元素,检查散列以查看它是否存在于第一个列表中.如果是,则将其作为交集的元素输出.

You could put all elements of the first list into a hash set. Then, iterate the second one and, for each of its elements, check the hash to see if it exists in the first list. If so, output it as an element of the intersection.

这篇关于高效的列表交集算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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