我试图解决一些hackerrank问题,我遇到了一个与multiset相关的问题。我试过这段代码,但我有点困惑我在哪一点上犯了错误?
class Multiset:
def __init__(self):
self.items=[]
def add(self, val):
# adds one occurrence of val from the multiset, if any
self.items.append(val)
def remove(self, val):
# removes one occurrence of val from the multiset, if any
if len(self.items):
if val in self.items:
self.items.remove(val)
def __contains__(self, val):
if val in self.items:
return True
return False
def __len__(self):
# returns the number of elements in the multiset
return len(self.items)
if __name__ == '__main__':
Run Code Online (Sandbox Code Playgroud)