classRandomizedSet:def__init__(self):""" Initialize your data structure here. """self.a=[]self.m={}definsert(self,val:int)->bool:""" Inserts a value to the set. Returns true if the set did not already contain the specified element. """ifvalinself.m:returnFalseself.m[val]=len(self.a)self.a.append(val)returnTruedefremove(self,val:int)->bool:""" Removes a value from the set. Returns true if the set contained the specified element. """ifvalinself.m:idx=self.m[val]self.a[idx],self.a[-1]=self.a[-1],self.a[idx]self.m[self.a[idx]]=idxself.a.pop()delself.m[val]returnTruereturnFalsedefgetRandom(self)->int:""" Get a random element from the set. """returnrandom.choice(self.a)# Your RandomizedSet object will be instantiated and called as such:# obj = RandomizedSet()# param_1 = obj.insert(val)# param_2 = obj.remove(val)# param_3 = obj.getRandom()
classRandomizedSet{privatefinalMap<Integer,Integer>m;privatefinalList<Integer>a;/** Initialize your data structure here. */publicRandomizedSet(){this.m=newHashMap<>();this.a=newArrayList<>();}/** * Inserts a value to the set. Returns true if the set did not already contain the specified * element. */publicbooleaninsert(intval){if(this.m.containsKey(val)){returnfalse;}this.m.put(val,this.a.size());this.a.add(val);returntrue;}/** Removes a value from the set. Returns true if the set contained the specified element. */publicbooleanremove(intval){if(this.m.containsKey(val)){intidx=this.m.get(val),last=this.a.size()-1;Collections.swap(this.a,idx,last);this.m.put(this.a.get(idx),idx);this.a.remove(last);this.m.remove(val);returntrue;}returnfalse;}/** Get a random element from the set. */publicintgetRandom(){returnthis.a.get(ThreadLocalRandom.current().nextInt(this.a.size()));}}/** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet obj = new RandomizedSet(); * boolean param_1 = obj.insert(val); * boolean param_2 = obj.remove(val); * int param_3 = obj.getRandom(); */
classRandomizedSet{unordered_map<int,int>mp;vector<int>nums;public:RandomizedSet(){}boolinsert(intval){if(mp.count(val))returnfalse;mp[val]=nums.size();nums.push_back(val);returntrue;}boolremove(intval){if(!mp.count(val))returnfalse;intremoveIndex=mp[val];nums[removeIndex]=nums.back();mp[nums.back()]=removeIndex;mp.erase(val);nums.pop_back();returntrue;}intgetRandom(){returnnums[rand()%nums.size()];}};/** * Your RandomizedSet object will be instantiated and called as such: * RandomizedSet* obj = new RandomizedSet(); * bool param_1 = obj->insert(val); * bool param_2 = obj->remove(val); * int param_3 = obj->getRandom(); */
classRandomizedSet{privatevarm:[Int:Int]privatevara:[Int]init(){self.m=[Int:Int]()self.a=[Int]()}funcinsert(_val:Int)->Bool{ifm[val]!=nil{returnfalse}m[val]=a.counta.append(val)returntrue}funcremove(_val:Int)->Bool{ifletidx=m[val]{letlast=a.count-1ifidx!=last{a.swapAt(idx,last)m[a[idx]]=idx}a.removeLast()m.removeValue(forKey:val)returntrue}returnfalse}funcgetRandom()->Int{returna[Int.random(in:0..<a.count)]}}/* let obj = RandomizedSet()* let param_1 = obj.insert(val)* let param_2 = obj.insert(val)* let param_3 = obj.remove(val)* let param_4 = obj.getRandom()*/