Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/705.html
705. Design HashSet
Level
Easy
Description
Design a HashSet without using any built-in hash table libraries.
To be specific, your design should include these functions:
add(value)
: Insert a value into the HashSet.contains(value)
: Return whether the value exists in the HashSet or not.remove(value)
: Remove a value in the HashSet. If the value does not exist in the HashSet, do nothing.
Example:
MyHashSet hashSet = new MyHashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.contains(1); // returns true
hashSet.contains(3); // returns false (not found)
hashSet.add(2);
hashSet.contains(2); // returns true
hashSet.remove(2);
hashSet.contains(2); // returns false (already removed)
Note:
- All values will be in the range of
[0, 1000000]
. - The number of operations will be in the range of
[1, 10000]
. - Please do not use the built-in HashSet library.
Solution
Since all values are in the range of [0, 1000000]
, create an array with length 1000001, which has boolean
type.
If a value is added, set the element at the corresponding index in the array to true
. If a value is removed, set the element at the corresponding index in the array to false
.
For the contains
function, simply return the element at the corresponding index in the array, where true
means the element exists in the HashSet and false
means the element does not exist in the HashSet.
-
public class Design_HashSet { class MyHashSet { boolean[] set; /** Initialize your data structure here. */ public MyHashSet() { set = new boolean[1000001]; } public void add(int key) { set[key]=true; } public void remove(int key) { set[key]=false; } /** Returns true if this set contains the specified element */ public boolean contains(int key) { return set[key]; } } /** * Your MyHashSet object will be instantiated and called as such: * MyHashSet obj = new MyHashSet(); * obj.add(key); * obj.remove(key); * boolean param_3 = obj.contains(key); */ } ############ class MyHashSet { private boolean[] data = new boolean[1000001]; public MyHashSet() { } public void add(int key) { data[key] = true; } public void remove(int key) { data[key] = false; } public boolean contains(int key) { return data[key]; } } /** * Your MyHashSet object will be instantiated and called as such: * MyHashSet obj = new MyHashSet(); * obj.add(key); * obj.remove(key); * boolean param_3 = obj.contains(key); */
-
// OJ: https://leetcode.com/problems/design-hashset/ // Time: O(1) // Space: O(1) class MyHashSet { bitset<1000001> bs; public: MyHashSet() {} void add(int key) { bs.set(key); } void remove(int key) { bs.reset(key); } bool contains(int key) { return bs.test(key); } };
-
class MyHashSet: def __init__(self): self.data = [False] * 1000001 def add(self, key: int) -> None: self.data[key] = True def remove(self, key: int) -> None: self.data[key] = False def contains(self, key: int) -> bool: return self.data[key] # Your MyHashSet object will be instantiated and called as such: # obj = MyHashSet() # obj.add(key) # obj.remove(key) # param_3 = obj.contains(key) ############ class MyHashSet: def __init__(self): """ Initialize your data structure here. """ self.buckets = 1000 self.itemsPerBucket = 1001 self.table = [[] for _ in range(self.buckets)] def hash(self, key): return key % self.buckets def pos(self, key): return key // self.buckets def add(self, key): """ :type key: int :rtype: void """ hashkey = self.hash(key) if not self.table[hashkey]: self.table[hashkey] = [0] * self.itemsPerBucket self.table[hashkey][self.pos(key)] = 1 def remove(self, key): """ :type key: int :rtype: void """ hashkey = self.hash(key) if self.table[hashkey]: self.table[hashkey][self.pos(key)] = 0 def contains(self, key): """ Returns true if this set did not already contain the specified element :type key: int :rtype: bool """ hashkey = self.hash(key) return (self.table[hashkey] != []) and (self.table[hashkey][self.pos(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)
-
type MyHashSet struct { data []bool } func Constructor() MyHashSet { data := make([]bool, 1000010) return MyHashSet{data} } func (this *MyHashSet) Add(key int) { this.data[key] = true } func (this *MyHashSet) Remove(key int) { this.data[key] = false } func (this *MyHashSet) Contains(key int) bool { return this.data[key] } /** * Your MyHashSet object will be instantiated and called as such: * obj := Constructor(); * obj.Add(key); * obj.Remove(key); * param_3 := obj.Contains(key); */
-
class MyHashSet { data: Array<boolean>; constructor() { this.data = new Array(10 ** 6 + 1).fill(false); } add(key: number): void { this.data[key] = true; } remove(key: number): void { this.data[key] = false; } contains(key: number): boolean { return this.data[key]; } } /** * Your MyHashSet object will be instantiated and called as such: * var obj = new MyHashSet() * obj.add(key) * obj.remove(key) * var param_3 = obj.contains(key) */