blob: 9077324ea8f5c7ebd15b35393a73b63166edb802 (
plain) (
tree)
|
|
#!/usr/bin/env python
import random
class RandomizedSet:
def __init__(self):
self.lst = []
self.idx_map = {}
def search(self, val):
return val in self.idx_map
def insert(self, val):
if self.search(val):
return False
self.lst.append(val)
self.idx_map[val] = len(self.lst) - 1
return True
def remove(self, val):
if not self.search(val):
return False
idx = self.idx_map[val]
self.lst[idx] = self.lst[-1]
self.idx_map[self.lst[-1]] = idx
self.lst.pop()
del self.idx_map[val]
return True
def getRandom(self):
return random.choice(self.lst)
def main():
pass
if __name__ == "__main__":
main()
|