[LeetCode] 380. Insert Delete GetRandom O(1)
竟然不需要import random,击败了5%的O(1)哈哈哈
class RandomizedSet:
def __init__(self):
self.data = []
def insert(self, val: int) -> bool:
if val in self.data:
return False
else:
self.data.append(val)
return True
def remove(self, val: int) -> bool:
if val in self.data:
self.data.remove(val)
return True
else:
return False
def getRandom(self) -> int:
return self.data[random.randint(0,len(self.data) -1)]
# 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()