题目链接
思路
使用队列进行保存
代码
public class ZigzagIterator {
Queue<Iterator<Integer>> queue = new LinkedList<>();
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
Iterator<Integer> it1 = v1.iterator();
Iterator<Integer> it2 = v2.iterator();
if(it1.hasNext()){
queue.add(it1);
}
if(it2.hasNext()){
queue.add(it2);
}
}
public int next() {
Iterator<Integer> it = queue.poll();
int v = it.next();
if(it.hasNext()){
queue.add(it);
}
return v;
}
public boolean hasNext() {
return !queue.isEmpty();
}
}
标签:hasNext,迭代,public,queue,add,281,it2,LeetCode
From: https://www.cnblogs.com/shixuanliu/p/17064500.html