← 返回 linkedin 的题目列表O(1) Time Insert, Remove, and RemoveRandom Data Structure
类型:online_judge
linkedin
Implement a class that supports the following operations in O(1) time complexity:
add(val): Inserts an integer val.
remove(val): Removes an integer val.
removeRandom(): Removes a random integer.
Assume all test cases' operations are valid, i.e., remove or removeRandom will not be called on an empty set.
Example:
add(1)
add(2)
removeRandom()
remove(1)
add(1)
The above sequence of operations may result in a set with remaining value [1].
Example
Input
add(1)
add(2)
removeRandom()
remove(1)
add(1)