There are many data structures commonly used as dynamic sets such as Binary Search Tree and Hash Table. The operations we need to support here are search() and insert(). For a self-balancing Binary Search Tree (TreeSet or TreeMap in Java), search() and insert() are both O(\log n)O(logn) time. For a Hash Table (HashSet or HashMap in Java), search() and insert() are both O(1)O(1) on average. Therefore, by using hash table, we can achieve linear time complexity for finding the duplicate in an unsorted array.
有许多数据结构常用作动态集,如二进制搜索树和哈希表。 我们需要在这里支持的操作是search()和insert()。 对于自平衡的二进制搜索树(TreeSet或Java中的TreeMap),search()和insert()都是O(\ log n)O(logn)时间。 对于散列表(HashSet或Java中的HashMap),search()和insert()平均都是O(1)O(1)。 因此,通过使用散列表,我们可以实现线性时间复杂度,以查找未排序数组中的重复数据。
网友评论