原题链接: https://leetcode.com/problems/peeking-iterator/
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().
Here is an 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.
package leetcode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class PeekingIterator implements Iterator<Integer> {
// https://leetcode.com/problems/peeking-iterator/
private Iterator<Integer> iterator;
private boolean flag = false; // 是否已经peek过
private Integer item;
public PeekingIterator(Iterator<Integer> iterator) {
this.iterator = iterator;
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
list.add(i);
}
Iterator<Integer> it = list.iterator();
PeekingIterator pi = new PeekingIterator(it);
while (pi.hasNext()) {
System.out.println(pi.peek() + "");
System.out.println(pi.next() + "");
}
}
/**
* Returns the next element in the iteration without advancing the iterator.
*
* @return
*/
public Integer peek() {
if (flag == false) {
Integer it = iterator.next();
flag = true;
item = it;
return item;
}
return item;
}
@Override
public Integer next() {
// TODO Auto-generated method stub
if (flag) {
flag = false;
return item;
} else {
return iterator.next();
}
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return flag || iterator.hasNext();
}
@Override
public void remove() {
iterator.remove();
}
}