Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RandomizedSet:
- def __init__(self):
- self.hashmap = {}
- self.list = []
- def insert(self, val: int) -> bool:
- if val in self.hashmap:
- return False
- self.list.append(val)
- self.hashmap[val] = len(self.list) - 1
- return True
- def remove(self, val: int) -> bool:
- if val not in self.hashmap:
- return False
- idx = self.hashmap[val]
- self.list[idx], self.list[-1] = self.list[-1], self.list[idx]
- self.hashmap[self.list[idx]] = idx
- self.list.pop()
- del self.hashmap[val]
- return True
- def getRandom(self) -> int:
- import random
- return random.choice(self.list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement