Python AC Solution with Set


  • 0
    N
    import random
    
    class RandomizedSet(object):
    
        def __init__(self):
            self.num = set()
            
        def insert(self, val):
            pre = len(self.num)
            self.num.add(val)        
            return True if pre != len(self.num) else False
    
        def remove(self, val):
            pre = len(self.num)
            self.num -= set([val])        
            return True if pre != len(self.num) else False
    
        def getRandom(self):
            return list(self.num)[random.randint(0, len(self.num) - 1)]
    

Log in to reply
 

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