【问题标题】:What is the time complexity if i converted HashSet to TreeSet如果我将 HashSet 转换为 TreeSet,时间复杂度是多少
【发布时间】:2020-10-14 16:42:31
【问题描述】:

如果我想使用构造函数创建一个新的 TreeSet,这种情况下的时间复杂度是多少?

HashSet<Integer> hashSet = new HashSet<>();

for (int i = 0; i < arr.length; i++){

    hashSet.add(arr[i]);

}

TreeSet<Integer> treeSet = new TreeSet<>(hashSet);

【问题讨论】:

    标签: java android time-complexity hashset treeset


    【解决方案1】:

    TreeSet(Collection) 构造函数一次只添加一个元素——添加每个元素,就像TreeSet 一样,需要 O(log n)。因此,整个操作耗时 O(n log n)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多