DY
Size: a a a
DY
A
MW
MW
MW
MW
DY
MW
A
MW
MW
MW
P
class MyHashSet:-----------------------------2 реализация------------------------
def __init__(self):
self.limit = 10000
self.buckets = [[] for _ in range(self.limit)]
def add(self, key: int) -> None:
if not self.contains(key):
bucket = self.buckets[self._bucket(key)]
bucket.append(key)
def remove(self, key: int) -> None:
bucket = self.buckets[self._bucket(key)]
if self.contains(key):
bucket.remove(key)
def _bucket(self, key):
return key % self.limit
def contains(self, key: int) -> bool:
bucket = self.buckets[self._bucket(key)]
for value in bucket:
if value == key:
return True
return False
class MyHashSet:
def get_bucket(self, key):
return self.data[key%10000]
def __init__(self):
self.data = [[]]*10000
def add(self, key):
arr = self.get_bucket(key)
if key in arr: return
arr.append(key)
def remove(self, key):
arr = self.get_bucket(key)
if key not in arr: return
arr.remove(key)
def contains(self, key):
arr = self.get_bucket(key)
return key in arr
OS
OS
OS
P
P
P
P