我正在修订我的考试排序和算法分析,和过去的试卷有以下问题:
“我希望能把所有的字母都用一些任意长的字母和字母和字谜(真词是由同样的字母构成的,例如狗和上帝)。”
My frst approach is this:
Find all the words by
Picking a first letter (N possibilities)
Picking a 2nd letter (N-1 possibilities)
Picking a 3rd letter (N-2 possibilities) etc.
For each word
Check it against a dictionary
这是逐行层次顺序遍历的代码。为什么时间复杂度是O(n)而不是O(n2)。 def levelOrder(根): queue = [root]
while len(queue):
count = len(queue)
while count:
current = queue.pop(0)
print(current.data,end='\t')
if current.left:
queue.append(current.left)
if current.right:
我知道红黑树只是一个平衡的二进制搜索树。所以我计算了元素数量为2^n的数据集的平均搜索成本(基本上是比较次数)。数据的设计方式是,它将形成完美的二进制搜索树。然而,在计算了平均成本后,我意识到红黑树的计算平均搜索成本略高于完全平衡的二进制搜索树。下面是我的表格:
# of elements Binary S. Tree Red-Black Tree
1 | 1 | 1
3 | 1.66667 | 1.6667
7 | 2.42857 |
我有一个n书籍的列表,例如,books = ["1984", "Harry Potter", "Hamlet"]。
另外,我有一个列表列表,其中每个内部列表都代表一个已经读过一些书(可能是全部或根本没有)的人,可能是0,也可能是多次阅读,因此,例如,我可以有一些类似于readings = [["1984", "Harry Potter", "Harry Potter"], ["Hamlet", "Harry Potter"], [], ["Hamlet"
void level_order_recursive(struct node *t , int h) //'h' is height of my binary tree
{ //'t' is address of root node
for(int i = 0 ; i <= h ; i++)
{
print_level(t , i);
}
}
每次调用print_level()之后,我认为递归函数被调用(2^i)次
我有下面的代码来模拟算法的递归行为,因为我没有计算出该算法的时间复杂度:
int M(int n)
{
int result = 1;
for (int i = n-1; i >= 0; --i)
{
result += M(i);
}
return result;
}
根据我的理解,我画了下面的树来说明算法:
(图片中的输入n为3)。我认为树中的节点数就是算法的复杂度。如果输入是n,时间复杂度是多少?谢谢!