<分区>
我有这个 for 循环:
public void method(int[] arr) {
Set set = new HashSet();
for(int i = 0; i < arr.length; i++){
set.add(arr[i]);
}
}
这个方法是O(n)吗?
<分区>
我有这个 for 循环:
public void method(int[] arr) {
Set set = new HashSet();
for(int i = 0; i < arr.length; i++){
set.add(arr[i]);
}
}
这个方法是O(n)吗?
最佳答案
如果您使用 HashSet,是。
HashSet
具有 O(1)
,您可以使用 for 循环将其与 O(n)
相乘。因此,整个构造具有 O(n)
。
关于java - 带 Set 的循环,是 O(n) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15459455/