At most 104 calls will be made to add, remove, and contains.
Solutions
Solution 1
1 2 3 4 5 6 7 8 910111213141516171819
classMyHashSet:def__init__(self):self.data=[False]*1000001defadd(self,key:int)->None:self.data[key]=Truedefremove(self,key:int)->None:self.data[key]=Falsedefcontains(self,key:int)->bool:returnself.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)
classMyHashSet{privateboolean[]data=newboolean[1000001];publicMyHashSet(){}publicvoidadd(intkey){data[key]=true;}publicvoidremove(intkey){data[key]=false;}publicbooleancontains(intkey){returndata[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); */
classMyHashSet{public:booldata[1000001];MyHashSet(){memset(data,false,sizeofdata);}voidadd(intkey){data[key]=true;}voidremove(intkey){data[key]=false;}boolcontains(intkey){returndata[key];}};/** * Your MyHashSet object will be instantiated and called as such: * MyHashSet* obj = new MyHashSet(); * obj->add(key); * obj->remove(key); * bool param_3 = obj->contains(key); */
typeMyHashSetstruct{data[]bool}funcConstructor()MyHashSet{data:=make([]bool,1000010)returnMyHashSet{data}}func(this*MyHashSet)Add(keyint){this.data[key]=true}func(this*MyHashSet)Remove(keyint){this.data[key]=false}func(this*MyHashSet)Contains(keyint)bool{returnthis.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); */
classMyHashSet{data:Array<boolean>;constructor(){this.data=newArray(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{returnthis.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) */
classMyHashSet:def__init__(self):self.size=1000self.data=[[]for_inrange(self.size)]defadd(self,key:int)->None:ifself.contains(key):returnidx=self.hash(key)self.data[idx].append(key)defremove(self,key:int)->None:ifnotself.contains(key):returnidx=self.hash(key)self.data[idx].remove(key)defcontains(self,key:int)->bool:idx=self.hash(key)returnany(v==keyforvinself.data[idx])defhash(self,key)->int:returnkey%self.size# Your MyHashSet object will be instantiated and called as such:# obj = MyHashSet()# obj.add(key)# obj.remove(key)# param_3 = obj.contains(key)
classMyHashSet{privatestaticfinalintSIZE=1000;privateLinkedList[]data;publicMyHashSet(){data=newLinkedList[SIZE];for(inti=0;i<SIZE;++i){data[i]=newLinkedList<Integer>();}}publicvoidadd(intkey){if(contains(key)){return;}intidx=hash(key);data[idx].addFirst(key);}publicvoidremove(intkey){if(!contains(key)){return;}intidx=hash(key);data[idx].remove(Integer.valueOf(key));}publicbooleancontains(intkey){intidx=hash(key);Iterator<Integer>it=data[idx].iterator();while(it.hasNext()){Integere=it.next();if(e==key){returntrue;}}returnfalse;}privateinthash(intkey){returnkey%SIZE;}}/** * 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); */
classMyHashSet{private:intsize=1000;vector<list<int>>data;public:MyHashSet():data(size){}voidadd(intkey){if(contains(key)){return;}intidx=hash(key);data[idx].push_back(key);}voidremove(intkey){if(!contains(key)){return;}intidx=hash(key);data[idx].remove(key);}boolcontains(intkey){intidx=hash(key);for(autoit=data[idx].begin();it!=data[idx].end();it++){if((*it)==key){returntrue;}}returnfalse;}inthash(intkey){returnkey%size;}};/** * Your MyHashSet object will be instantiated and called as such: * MyHashSet* obj = new MyHashSet(); * obj->add(key); * obj->remove(key); * bool param_3 = obj->contains(key); */
typeMyHashSetstruct{data[]list.List}funcConstructor()MyHashSet{returnMyHashSet{make([]list.List,1000)}}func(this*MyHashSet)Add(keyint){ifthis.Contains(key){return}idx:=this.hash(key)this.data[idx].PushBack(key)}func(this*MyHashSet)Remove(keyint){idx:=this.hash(key)fore:=this.data[idx].Front();e!=nil;e=e.Next(){ife.Value.(int)==key{this.data[idx].Remove(e)}}}func(this*MyHashSet)Contains(keyint)bool{idx:=this.hash(key)fore:=this.data[idx].Front();e!=nil;e=e.Next(){ife.Value.(int)==key{returntrue}}returnfalse}func(this*MyHashSet)hash(keyint)int{returnkey%len(this.data)}/** * Your MyHashSet object will be instantiated and called as such: * obj := Constructor(); * obj.Add(key); * obj.Remove(key); * param_3 := obj.Contains(key); */