2.3 时间复杂度 1. 常数阶 0(1) C 语言代码中出现多余的外部变量 i
作者: mazezen创建于 2026年5月18日更新于 2026年5月18日
Location: Chapter 2, Section 3, Time complexity
- Constant time O(1) ¶
The number of operations in constant time is independent of the size of the input data, that is, it does not change with the change of n.
In the following functions, although the number of operations size may be large, but because it is independent of the size of the input data n, the time complexity is still:
/* Constant time */ int constant(int n) { int count = 0; int size = 100000; int i = 0; for (int i = 0; i < size; i++) { count++; } return count; }
Description: In the for loop, int i = 0 defines a new local variable i, whose scope is only within the for loop. That is, the outer int i = 0; is not used at all, and it can be omitted. After the loop ends, the inner i disappears, and the outer i still exists, but is not used in your function.
内容来源: krahets/hello-algo