← 返回 databricks 的题目列表Design a Snapshot Set Iterator
类型:online_judge
Design a Snapshot Set Iterator
Design an integer SnapshotSet supporting:
add(n) -> bool
remove(n) -> bool
contains(n) -> bool
getIterator() -> SnapshotIterator
A SnapshotIterator must expose exactly the set contents at the time it was created, in insertion order. Later additions and removals must not affect existing iterators, and iterators must be independent.
hasNext() -> bool
next() -> int
Requirements
add and remove are amortized O(1).
Creating an iterator is O(1) and cannot copy the set.
Total space is O(N + M), where N is the number of successful historical insertion records and M is the number of live iterators.
Removing and then re-adding a value creates a new insertion record at the end of insertion order.
Input format
Commands are add n, remove n, contains n, iter, has id, and next id. Print results as specified.
Example
Input
7
add 1
add 2
iter
remove 1
has 0
next 0
next 0
Output
True
True
0
True
True
1
2