← 返回 bloomberg 的题目列表Insert Delete GetRandom O(1)
类型:online_judge
Implement RandomizedSet supporting average O(1) time operations:
insert(val) -> bool: Insert val if not present and return True; otherwise return False.
remove(val) -> bool: Remove val if present and return True; otherwise return False.
getRandom() -> int: Return a random element from the set with equal probability. The set is guaranteed non-empty when called.
Constraints: number of operations 1 <= ops <= 2*10^5, values are 32-bit integers.
Example
Input
insert(1)
remove(2)
insert(2)
getRandom()
Output
True
False
True
(returns 1 or 2)