-1
我想在python中实现队列列表(自我实现)的检查成员方法,而不必遍历所有成员来检查并且不牺牲顺序。如何在恒定时间O(1)中为队列列表实现检查成员方法?
我有以下FIFOqueue实现:
class FIFOqueue(object):
"""Initialize with a list"""
def __init__(self, elements):
self.list = elements
def pop(self):
"""Return the oldest element of the list (last index)"""
return self.list.pop()
def insert(self, element):
"""Insert a new element at the end"""
self.list = self.list + [element]
如何在恒定时间O(1)添加到上面的类进行检查成员方法?
你不能;这就是为什么'dict's存在。 – chepner