如何在常数时间内查找 N 大小列表中的整数?

How to lookup an integer in an N sized list in constant time?

列表的属性:

  1. 的大小必须为 N,其中 N 是整数的数量

  2. 没有空单元格

  3. 数字可能不是完全连续的(即 {-23,-15,-3,1,2,6,7,8,15,100})

  4. Insertion/Lookup需要常数时间。

我的第一直觉是使用散列 table,但这会创建未使用的单元格,其中数字被跳过。

有没有什么方法可以构建这样的列表来检查列表中是否存在数字?

根据我的意见,您可以使用 Set,具体取决于您的具体用例,如果您需要维护 Java 的 HashSet or LinkedHashSet根据文档应该是恒定时间的顺序:

Like HashSet, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets.

如果您正在寻找其他平台上的解决方案,也许有等效的实现,或者您可以查看 Java 的源代码并自行实现。