且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

a.insert(0,x)是o(n)函数吗? a.append是O(1)函数吗? Python

更新时间:2021-08-04 23:11:02

insert在列表的第0个索引处需要移位其他所有元素,使其沿其进行O(N)操作.但是,如果您使用 deque ,则此操作为O(1 ).

insert at the 0th index of a list requires shifting every other element along which makes it an O(N) operation. However, if you use a deque this operation is O(1).

append是摊销的O(1)运算,因为它只需要将项目添加到列表的末尾,并且不进行任何移位.有时列表需要增长,因此它并不总是O(1)操作.

append is an amortized O(1) operation since it simply requires adding the item on to the end of the list and no shifting is done. Sometimes the list needs to grow so it is not always an O(1) operation.