website/content/ChapterFour/0200~0299/0284.Peeking-Iterator.md
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation -- it essentially peek() at the element that will be returned by the next call to next().
Example:
Assume that the iterator is initialized to the beginning of the list: [1,2,3].
Call next() gets you 1, the first element in the list.
Now you call peek() and it returns 2, the next element. Calling next() after that still return 2.
You call next() the final time and it returns 3, the last element.
Calling hasNext() after that should return false.
Follow up: How would you extend your design to be generic and work with all types, not just integer?
给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。
peek() 是偷看的意思。偷偷看一看下一个元素是什么,但是并不是 next() 访问。
package leetcode
//Below is the interface for Iterator, which is already defined for you.
type Iterator struct {
}
func (this *Iterator) hasNext() bool {
// Returns true if the iteration has more elements.
return true
}
func (this *Iterator) next() int {
// Returns the next element in the iteration.
return 0
}
type PeekingIterator struct {
nextEl int
hasEl bool
iter *Iterator
}
func Constructor(iter *Iterator) *PeekingIterator {
return &PeekingIterator{
iter: iter,
}
}
func (this *PeekingIterator) hasNext() bool {
if this.hasEl {
return true
}
return this.iter.hasNext()
}
func (this *PeekingIterator) next() int {
if this.hasEl {
this.hasEl = false
return this.nextEl
} else {
return this.iter.next()
}
}
func (this *PeekingIterator) peek() int {
if this.hasEl {
return this.nextEl
}
this.hasEl = true
this.nextEl = this.iter.next()
return this.nextEl
}