HTMLify

LeetCode - Insert Delete GetRandom O(1) - Python
Views: 114 | Author: abh
from random import choice
class RandomizedSet:

    def __init__(self):
        self.values = []

    def insert(self, val: int) -> bool:
        if val in self.values:
            return False
        self.values.append(val)
        return True
        
    def remove(self, val: int) -> bool:
        if val in self.values:
            self.values.remove(val)
            return True
        return False

    def getRandom(self) -> int:
        return choice(self.values)


# 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()

Comments