from collections import defaultdict
dict1 = defaultdict(int)
dict2 = defaultdict(set)
dict3 = defaultdict(str)
dict4 = defaultdict(list)
# 如果key为空,返回int,set,str,list的默认空值
class MyHashSet:
def __init__(self):
"""
Initialize your data structure here.
"""
self.keyRange = 769 # 质数
self.bucketArray = [Bucket() for i in range(self.keyRange)]
def _hash(self, key):
return key % self.keyRange
def add(self, key: int) -> None:
bucketIndex = self._hash(key)
self.bucketArray[bucketIndex].insert(key)
def remove(self, key: int) -> None:
bucketIndex = self._hash(key)
self.bucketArray[bucketIndex].delete(key)
def contains(self, key: int) -> bool:
"""
Returns true if this set contains the specified element
"""
bucketIndex = self._hash(key)
return self.bucketArray[bucketIndex].exists(key)
class Node:
def __init__(self, value, nextNode=None):
self.value = value
self.next = nextNode
class Bucket:# 在头部插入
'''
哈希集合的元素
'''
def __init__(self):
self.head = Node(-1)
def exists(self, val):
cur = self.head.next
while cur!=None:
if cur.value==val:
return True
cur = cur.next
return False
def insert(self, newVal):
if not self.exists(newVal):
newNode = Node(newVal, self.head.next)
self.head.next = newNode
def delete(self, val):
pre = self.head
cur = self.head.next
while cur is not None:
if cur.value == val:
pre.next = cur.next
return
pre = cur
cur = cur.next
self.tripleStack = [None]*stackSize*3
self.top = [0, stackSize , stackSize*2] # 栈顶指针,也是下一个数据的存放位置
self.roof = [stackSize, stackSize*2, stackSize*3] # 栈顶指针最大位置
self.bottom = [0, stackSize, stackSize*2] # 栈顶指针最小位置