You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classRandomizedSet(object):
def__init__(self):
""" Initialize your data structure here. """self.vals= []
self.idxs= {}
definsert(self, val):
""" Inserts a value to the set. Returns true if the set did not already contain the specified element. :type val: int :rtype: bool """ifvalnotinself.idxs:
self.vals.append(val)
self.idxs[val] =len(self.vals) -1returnTrueelse:
returnFalsedefremove(self, val):
""" Removes a value from the set. Returns true if the set contained the specified element. :type val: int :rtype: bool """ifvalinself.idxs:
cur_out=self.idxs[val]
cur_in=self.vals[-1]
self.vals[cur_out] =cur_inself.idxs[cur_in] =cur_outself.vals.pop()
delself.idxs[val]
returnTrueelse:
returnFalsedefgetRandom(self):
""" Get a random element from the set. :rtype: int """importrandomreturnrandom.choice(self.vals)
# 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()