我对这段代码的时间复杂度和用于查找它的逻辑感到困惑。
void doit(int N) {
for (int k = 1; k < N; k *= 2) { <----I am guessing this runs O(logN)
for (int j = 1; j < k; j += 1) { <------I am not sure how this one works.
}
}
}
我已经尝试过手写解决它。但是,我还是不明白。
感谢您的时间。
编辑:
添加另一个问题。相同的概念,不同的形式。
void doit(int N) {
int j, k; //I ended up getting this answer to be O(n^(n/2)) But then I was stuck after that...is that even the right answer?
for (k = 1; k < N / 2; k += 1) {
for (j = k; j < 2 * k; j += 1) [
x[j] = x[j-k] + x[j];//This doesn't really matter
}
}
}