requirements

Design a HashSet without using any built-in hash table libraries.

Implement MyHashSet class:

Void add(key) Inserts the value key into the hash set. Bool Contains (key) returns whether the hash set contains the value key. Void Remove (key) Removes the given value key from the hash set. If it’s not in the hash set, do nothing. Example:

Input:  ["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"] [[], [1], [2], [1], [3], [2], [2], [2]] Output: [NULL, NULL, NULL, true, false, NULL, true, NULL, false]  MyHashSet myHashSet = new MyHashSet(); myHashSet.add(1); // set = [1] myHashSet.add(2); // set = [1, 2] myHashSet.contains(1); // Returns True myhashset. contains(3); // Return False, (not found) myHashSet.add(2); // set = [1, 2] myHashSet.contains(2); // Return True myHashset.remove (2); // set = [1] myHashSet.contains(2); // Return False, (removed)Copy the code

Tip:

  • 0 <= key <= 106
  • Add, remove, and Contains are called a maximum of 104 times.

The core code

class MyHashSet:

    def __init__(self) :
        self.list = [0 for _ in range(1000005)]

    def add(self, key: int) - >None:
        self.list[key] = 1

    def remove(self, key: int) - >None:
        self.list[key] = 0

    def contains(self, key: int) - >bool:
        return self.list[key] == 1

# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)
Copy the code

It is easy to construct a data structure that fits the meaning of the question.