Redian新闻
>
算法题:两列找共同元素有O(n)的算法吗?
avatar
算法题:两列找共同元素有O(n)的算法吗?# JobHunting - 待字闺中
c*e
1
Given two arrays, each with distinct integers, give an efficient
algorithm to find out the common elements.
显然我们可以对两个数列进行快速排序。但有更简单的算法吗?
avatar
C*y
2
hash

【在 c**********e 的大作中提到】
: Given two arrays, each with distinct integers, give an efficient
: algorithm to find out the common elements.
: 显然我们可以对两个数列进行快速排序。但有更简单的算法吗?

avatar
P*l
3
hash
avatar
a*a
4
What if the arrays are huge?
We can use bitset for hash to save space but then what if there are
duplicates in each array?
We can not use one bit to represent a number since it may have duplicates.
What to do?
Thanks.
avatar
A*H
5
of course you can, when you see duplicates in first array, just don't do
anything, when you see dup in second array, it's a common number

【在 a*****a 的大作中提到】
: What if the arrays are huge?
: We can use bitset for hash to save space but then what if there are
: duplicates in each array?
: We can not use one bit to represent a number since it may have duplicates.
: What to do?
: Thanks.

avatar
f*4
6
如果数据没多到内存里放不下hash信息的话,用hashmap就能处理重复
(key, value)扫描A的术后,第一次插入(key, 1)有重复的话,对++value
B扫描,--value,直到0,都是重复的
如果内存放不下hash信息了,只能用mergsort,对2个array进行外部排序
然后从2个array里面取同等大小数据段,扫描,直到任意array空

【在 a*****a 的大作中提到】
: What if the arrays are huge?
: We can use bitset for hash to save space but then what if there are
: duplicates in each array?
: We can not use one bit to represent a number since it may have duplicates.
: What to do?
: Thanks.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。