Duplicate Numbers The following program can find out if an integer array contains duplicate numbers. Give the best-case and worst-case runtime complexity using Big-O notation. public static boolean containsDuplicates (int[] array, int number) { int n = array.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i !== j && array[i] === array[j]) { return true; } } } return false; } public static void main ( String[] args ) { int[] randomNumbers = { 10, 2, 9, 15, 22 }; containsDuplicates(randomNumbers, 27); } 1: Best Case 2: Worst Case匹配题
A
O(n log n)
B
O(n^2)
C
O(1)
D
O(n)
登录即可查看完整答案
我们收录了全球超50000道真实原题与详细解析,现在登录,立即获得答案。
类似问题
Suppose we have a recursive function defined on a binary tree. The function takes as input a node in the tree, calls another function bar that performs some work on the node and then makes recursive calls to itself on the left and right subtrees provided they are not empty. def foo(u): bar(u) if u.left is not empty then foo(u.left) if u.right is not empty then foo(u.right) Suppose bar(u) takes O(|subtree(u)|) time to execute, where |subtree(u)| is the size of the subtree rooted at u. What is the time complexity of running foo from the root a tree with n nodes. Select the tightest bound that holds.
Suppose we have a recursive function defined on a tree. The function takes as input a node in the tree, calls another function bar that performs some work on the node and then makes recursive calls to itself on the parent provided it has one. def foo(u): bar(u) if u.parent is not empty then foo(u.parent) Suppose bar(u) takes O(1) time to execute. What is the time complexity of running foo from a leaf of a tree with n nodes. Select the tightest bound that holds.
What is the big-O of the worst-case time cost of inserting an element somewhere into an array of size n? Select the tightest bound that holds.
Which of the following is a big-O description that applies to the function g(n) = 18log n + 2n + 3n log n? Select the tightest bound that holds.
更多留学生实用工具
希望你的学习变得更简单
加入我们,立即解锁 海量真题 与 独家解析,让复习快人一步!