← 返回 apple 的题目列表Insert Delete GetRandom O(1)
类型:qbank
Implement the RandomizedSet class:.
Examples
Example 1:
Input: ["RandomizedSet","insert","remove","insert","getRandom","remove","insert","getRandom"] [[],[1],[2],[2],[],[1],[2],[]]
Output: [null,true,false,true,2,true,false,2]
Explanation:
Insert 1 → true. Remove 2 → false (not present). Insert 2 → true. getRandom returns 1 or 2 uniformly. Remove 1 → true. Insert 2 → false (already present). getRandom returns 2 (only element left).
Constraints
-2^31 <= val <= 2^31 - 1
At most 2 * 10^5 calls will be made to insert, remove, and getRandom
There will be at least one element in the data structure when getRandom is called