← 返回 bytedance 的题目列表O(1) Insert, Remove, and Weighted Random Retrieval Class
类型:online_judge
bytedance
Implement a class with the following methods:
insert(number): Returns false if the number already exists; otherwise, inserts the number and returns true.
remove(number): Returns false if the number doesn't exist; otherwise, removes the number and returns true.
getRandom(): Returns a random number based on weight, with equal probability for each number.
All methods must work in O(1) time complexity.
Please design and implement this class, and provide at least 5 test cases.
Example
Input
insert 1
insert 1
remove 1
remove 2
insert 2
getRandom