且构网

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

Java中设置的时间复杂度

更新时间:2022-06-04 03:11:31

我相信它的O(n)因为你循环遍历数组,并且包含 add 应该是常量时间,因为它是基于 hash 的集合。如果它不是基于散列的并且需要在整个集合上进行迭代来进行查找,则上限将是n ^ 2.

i believe its O(n) because you loop over the array, and contains and add should be constant time because its a hash based set. If it were not hash based and required iteration over the entire set to do lookups, the upper bound would be n^2.

整数是不可变的,因此空间复杂度会是2n,我认为简化为n,因为常数无关紧要。

Integers are immutable, so the space complexity would be 2n, which I believe simplifies to just n, since constants don't matter.

如果数组中有对象并设置,那么你将有2n个引用, n个对象,所以你是3n,它仍然是线性的(乘以常数)空间约束。

If you had objects in the array and set, then you would have 2n references and n objects, so you are at 3n, which is still linear (times a constant) space constraints.

EDIT-- yep这个类提供基本的恒定时间性能操作(添加,删除,包含和大小),假设散列函数在桶中正确地分散元素。

EDIT-- yep "This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets."

参见这里