← 返回 openai 的题目列表Resumable List Iterator with Save and Restore Functionality
类型:online_judge
openai
Design an interface and implement a resumable list iterator that supports the following functions:
next(): Returns the next element, or throws an exception if there are no more elements.
hasNext(): Returns whether the iterator has more elements.
save(): Saves the current state of the iterator.
restore(): Restores the iterator to the last saved state.
Implement a test method to verify the iterator's functionality.
Input Size:
The list length does not exceed $10^5$.
Example:
Suppose we have a list: [1, 2, 3, 4, 5]
- Call `next()` -> Returns `1`
- Call `save()` to save the state
- Call `next()` -> Returns `2`
- Call `next()` -> Returns `3`
- Call `restore()`
- Call `next()` again -> Returns `2`
Example
Input
[1, 2, 3, 4, 5]