且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找重复在一个阵列中的O(N)时间

更新时间:2023-10-05 16:25:10

我相信一个好的解决方案(体面的内存使用量,可用于立即确定是否一个条目已经看到这样preserving秩序,并用线性复杂度)是一个线索

I believe a good solution (decent memory usage, can be used to immediately determine if an entry has already been seen thus preserving order, and with a linear complexity) is a trie.

如果你插入的元素融入线索,好像他们是一个字符串,每个数字在每个节点(从MSD起),你可以用O(复杂度拉这一关的 M 的ñ )其中的 M 的是数字在以10位的平均长度。

If you insert the elements into the trie as if they were a string with each digit (starting from the MSD) in each node, you can pull this off with a complexity of O(m N) where m is the average length of numbers in base-10 digits.

您刚刚遍历所有条目,将其插入到该线索。每当一个元素已经存在,你跳过它并移动到下一个。在此重复(不像我的previous的基数排序的答案)的将会的立即在最后一次迭代中,而不是或什么不是。

You'd just loop over all your entries and insert them into the trie. Each time an element already exists, you skip it and move on to the next. Duplicates in this (unlike in my previous answer of a Radix Sort) will be found immediately instead of in the last iteration or what not.

我不知道,如果你将受益于使用后缀树在这里,因为基地的字符被输入到特里只有10(相对于基128 ANSI字符串),但它可能

I'm not sure if you would benefit from using a suffix tree here, as the "base" of the characters being entered into the trie is only 10 (compared to the base-128 for ANSI strings), but it's possible.