B2.4.1 · 效率与内存
辅助空间
Python · 逐步执行
辅助空间
比较持续累加的总和与不断增长的输入副本。
Python 实现
1def auxiliary_space(items, copy_input):2 total = 03 working = []4 for value in items:5 total = total + value6 if copy_input:7 working.append(value)8 return total 给定列表占用的是输入空间。在课堂模型中,total 和 value 占用固定数量的存储位置,而副本会增长到 n 个元素。不创建副本时,辅助空间为 O(1);创建副本时为 O(n)。两种遍历的时间复杂度均为 O(n)。