time complexity for a loop
问题 foo = [] i = 1 while i < n: foo= foo + ["a"] i*=2 What is the time complexity of this code? My thoguht is: the while loop does log(n) iteration. For each iteration new list is created. So, the total time complexity is: O(log^2(n)). Am I right? 回答1: The while loop iterates log(n) times. foo + ["a"] : Create a new list by copying the original list. According to Time Complexity - Python Wiki, copying a list take O(n). Time complexity => 1 + 2 + 3 + ... + log(n) => (log(n) + 1) * log(n) / 2 => O