小编Mik*_*eez的帖子

使用big-o进行时间复杂度分析

修改后,我们得出结论,时间复杂度实际上是O(2 ^ n)

问题是时间复杂度是多少?是O(2 ^ n)还是?

我认为这是因为for循环的原因被认为是n次运行.然后嵌套的while循环运行2 ^ n次.第二个while循环运行2 ^ n次.

Algorithm subsetsGenerator(T)     
Input:  A set T of n elements       
Output: All the subsets of T stored in a queue Q   {     

create an empty queue Q;      
create an empty stack S;     
let a1, a2, …,  an be all the elements in T;       
S.push({});    // Push the empty subset into the stack      
S.push({a1})         
for ( each element ai in T-{a1} )         
{ 
  while (S is not empty )                 
  {  
x=S.pop; …
Run Code Online (Sandbox Code Playgroud)

big-o

11
推荐指数
1
解决办法
170
查看次数

标签 统计

big-o ×1