← 返回 databricks 的题目列表Snapshot Set Iterator
类型:online_judge
Problem: Implement a Snapshot Set Iterator
Design a SnapshotSet that stores unique integers and can create independent snapshot iterators while the set continues to be modified.
Implement these operations:
add(x): Add integer x to the set. Do nothing if it already exists.
remove(x): Remove integer x from the set. Do nothing if it is absent.
iterator(): Create and return a new iterator. It must traverse all elements that were present in the set at the time iterator() was called.
next(id): Return the next element from iterator id; return END if that iterator is exhausted.
Snapshot semantics
Changes made through add or remove after an iterator is created must not affect that iterator.
Different iterators represent snapshots taken at different times.
Each element may be returned at most once by an iterator.
No particular element order is required, but the order for an individual iterator must remain consistent.
Input Format
The first line contains the number of operations Q.
Each of the next Q lines is one of:
add x
remove x
iterator
next id
An iterator operation must output the newly assigned iterator ID. IDs start at 0 and increase in creation order.
Output Format
For every iterator operation, print its new ID.
For every next id operation, print that iterator's next element, or END when it is exhausted.
Constraints
1 <= Q <= 2 * 10^5
x is a signed 32-bit integer.
Every ID used by next id is guaranteed to have been returned by an earlier iterator operation.
Example
Input
10
add 1
add 2
iterator
remove 1
add 3
next 0
next 0
iterator
next 1
next 1
Output
0
1
2
1
3
Iterator 0 snapshots {1, 2}. Its result is unaffected by subsequently removing 1 and adding 3. Iterator 1 snapshots {2, 3}.
Example
Input
10
add 1
add 2
iterator
remove 1
add 3
next 0
next 0
iterator
next 1
next 1
Output
0
1
2
1
3