(算法)在 O(n) 时间内查找两个未排序的数组是否有任何共同元素而不进行排序?

2024-03-20

We have two unsorted arrays and each array has a length of n. These arrays contain random integers in the range of 0-n100. How to find if these two arrays have any common elements in O(n)/linear time? Sorting is not allowed.


哈希表会拯救你。确实,它就像算法的瑞士刀。
只需将第一个数组中的所有值放入其中,然后检查第二个数组中是否存在任何值即可。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

(算法)在 O(n) 时间内查找两个未排序的数组是否有任何共同元素而不进行排序? 的相关文章

随机推荐