classZigzagIterator:def__init__(self,v1:List[int],v2:List[int]):self.cur=0self.size=2self.indexes=[0]*self.sizeself.vectors=[v1,v2]defnext(self)->int:vector=self.vectors[self.cur]index=self.indexes[self.cur]res=vector[index]self.indexes[self.cur]=index+1self.cur=(self.cur+1)%self.sizereturnresdefhasNext(self)->bool:start=self.curwhileself.indexes[self.cur]==len(self.vectors[self.cur]):self.cur=(self.cur+1)%self.sizeifself.cur==start:returnFalsereturnTrue# Your ZigzagIterator object will be instantiated and called as such:# i, v = ZigzagIterator(v1, v2), []# while i.hasNext(): v.append(i.next())
publicclassZigzagIterator{privateintcur;privateintsize;privateList<Integer>indexes=newArrayList<>();privateList<List<Integer>>vectors=newArrayList<>();publicZigzagIterator(List<Integer>v1,List<Integer>v2){cur=0;size=2;indexes.add(0);indexes.add(0);vectors.add(v1);vectors.add(v2);}publicintnext(){List<Integer>vector=vectors.get(cur);intindex=indexes.get(cur);intres=vector.get(index);indexes.set(cur,index+1);cur=(cur+1)%size;returnres;}publicbooleanhasNext(){intstart=cur;while(indexes.get(cur)==vectors.get(cur).size()){cur=(cur+1)%size;if(start==cur){returnfalse;}}returntrue;}}/** * Your ZigzagIterator object will be instantiated and called as such: * ZigzagIterator i = new ZigzagIterator(v1, v2); * while (i.hasNext()) v[f()] = i.next(); */