Python Solution


  • 0
    A

    '''
    class RandomizedSet(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.ds={}
    
    def insert(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
        """
        if val in self.ds:
            return False
        self.ds[val]=1
        return True
    
    def remove(self, val):
        """
        Removes a value from the set. Returns true if the set contained the specified element.
        :type val: int
        :rtype: bool
        """
        if val not in self.ds:
            return False
        del self.ds[val]
        return True
    
    def getRandom(self):
        """
        Get a random element from the set.
        :rtype: int
        """
        return random.choice(self.ds.keys())
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.