高效名单交集算法 [英] Efficient list intersection algorithm

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

问题描述

由于两个列表(不一定排序),什么是最有效的非递归算法找到这些列表的交集?

Given two lists (not necessarily sorted), what is the most efficient non-recursive algorithm to find the intersection of those lists?

推荐答案

您可以把第一个列表中的所有元素成为哈希集合。然后,进行迭代,第二个,并且对于它的每个元素,检查散列,看它是否存在于第一列表。如果是这样,将它输出作为交点的元素

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