classRandomizedSet:def__init__(self):self.d={}self.q=[]definsert(self,val:int)->bool:ifvalinself.d:returnFalseself.d[val]=len(self.q)self.q.append(val)returnTruedefremove(self,val:int)->bool:ifvalnotinself.d:returnFalsei=self.d[val]self.d[self.q[-1]]=iself.q[i]=self.q[-1]self.q.pop()self.d.pop(val)returnTruedefgetRandom(self)->int:returnchoice(self.q)# 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{privateMap<Integer,Integer>d=newHashMap<>();privateList<Integer>q=newArrayList<>();privateRandomrnd=newRandom();publicRandomizedSet(){}publicbooleaninsert(intval){if(d.containsKey(val)){returnfalse;}d.put(val,q.size());q.add(val);returntrue;}publicbooleanremove(intval){if(!d.containsKey(val)){returnfalse;}inti=d.get(val);d.put(q.get(q.size()-1),i);q.set(i,q.get(q.size()-1));q.remove(q.size()-1);d.remove(val);returntrue;}publicintgetRandom(){returnq.get(rnd.nextInt(q.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{public:RandomizedSet(){}boolinsert(intval){if(d.count(val)){returnfalse;}d[val]=q.size();q.push_back(val);returntrue;}boolremove(intval){if(!d.count(val)){returnfalse;}inti=d[val];d[q.back()]=i;q[i]=q.back();q.pop_back();d.erase(val);returntrue;}intgetRandom(){returnq[rand()%q.size()];}private:unordered_map<int,int>d;vector<int>q;};/** * 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(); */
typeRandomizedSetstruct{dmap[int]intq[]int}funcConstructor()RandomizedSet{returnRandomizedSet{map[int]int{},[]int{}}}func(this*RandomizedSet)Insert(valint)bool{if_,ok:=this.d[val];ok{returnfalse}this.d[val]=len(this.q)this.q=append(this.q,val)returntrue}func(this*RandomizedSet)Remove(valint)bool{if_,ok:=this.d[val];!ok{returnfalse}i:=this.d[val]this.d[this.q[len(this.q)-1]]=ithis.q[i]=this.q[len(this.q)-1]this.q=this.q[:len(this.q)-1]delete(this.d,val)returntrue}func(this*RandomizedSet)GetRandom()int{returnthis.q[rand.Intn(len(this.q))]}/** * Your RandomizedSet object will be instantiated and called as such: * obj := Constructor(); * param_1 := obj.Insert(val); * param_2 := obj.Remove(val); * param_3 := obj.GetRandom(); */
classRandomizedSet{privated:Map<number,number>=newMap();privateq:number[]=[];constructor(){}insert(val:number):boolean{if(this.d.has(val)){returnfalse;}this.d.set(val,this.q.length);this.q.push(val);returntrue;}remove(val:number):boolean{if(!this.d.has(val)){returnfalse;}consti=this.d.get(val)!;this.d.set(this.q[this.q.length-1],i);this.q[i]=this.q[this.q.length-1];this.q.pop();this.d.delete(val);returntrue;}getRandom():number{returnthis.q[Math.floor(Math.random()*this.q.length)];}}/** * Your RandomizedSet object will be instantiated and called as such: * var obj = new RandomizedSet() * var param_1 = obj.insert(val) * var param_2 = obj.remove(val) * var param_3 = obj.getRandom() */
userand::Rng;usestd::collections::HashSet;structRandomizedSet{list:HashSet<i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implRandomizedSet{fnnew()->Self{Self{list:HashSet::new(),}}fninsert(&mutself,val:i32)->bool{self.list.insert(val)}fnremove(&mutself,val:i32)->bool{self.list.remove(&val)}fnget_random(&self)->i32{leti=rand::thread_rng().gen_range(0,self.list.len());*self.list.iter().collect::<Vec<&i32>>()[i]}}
publicclassRandomizedSet{privateDictionary<int,int>d=newDictionary<int,int>();privateList<int>q=newList<int>();publicRandomizedSet(){}publicboolInsert(intval){if(d.ContainsKey(val)){returnfalse;}d.Add(val,q.Count);q.Add(val);returntrue;}publicboolRemove(intval){if(!d.ContainsKey(val)){returnfalse;}inti=d[val];d[q[q.Count-1]]=i;q[i]=q[q.Count-1];q.RemoveAt(q.Count-1);d.Remove(val);returntrue;}publicintGetRandom(){returnq[newRandom().Next(0,q.Count)];}}/** * 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(); */