原因是在next()第一行 Iterator 应当写成 Iterator<Integer>
这里用LinkedList是为了扩展到k个list
如果只是两个list的话可以简单地用一个swap
public int next() { if (j.hasNext()) { tmp = j; j = i; i = tmp; } return i.next(); }
Line 11: error: incompatible types: Object cannot be converted to int
28.58%public class ZigzagIterator {
private LinkedList<Iterator<Integer>> list;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
list = new LinkedList<Iterator<Integer>>();
if (!v1.isEmpty()) list.add(v1.iterator());
if (!v2.isEmpty()) list.add(v2.iterator());
}
public int next() {
Iterator<Integer> iter = list.remove();
int result = iter.next();
if (iter.hasNext()) list.add(iter);
return result;
}
public boolean hasNext() {
return !list.isEmpty();
}
}
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/
No comments:
Post a Comment