What is the complexity of this code whose nested for loop repeatedly doubles its counter?
问题 In the book Programming Interviews Exposed it says that the complexity of the program below is O(N), but I don't understand how this is possible. Can someone explain why this is? int var = 2; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j *= 2) { var += var; } } 回答1: You need a bit of math to see that. The inner loop iterates Θ(1 + log [N/(i+1)]) times (the 1 + is necessary since for i >= N/2 , [N/(i+1)] = 1 and the logarithm is 0, yet the loop iterates once). j takes the values (i