Analysis:
define a dictionary to store the elements and a vector to store the index of them.
the key of removal is to move the tail element to the slot of removal element and update the tail element index.
Time Complexity:
- O(1)
Space Complexity:
- O(n)
Code
1 | class RandomizedSet(object): |