给出两个一维的向量,请你实现一个迭代器,交替返回它们中间的元素。
示例:
输入: v1 = [1,2] v2 = [3,4,5,6] 输出:[1,3,2,4,5,6] 解析:
通过连续调用 next 函数直到 hasNext 函数返回false,
next 函数返回值的次序应依次为:[1,3,2,4,5,6]。
拓展:假如给你 k
个一维向量呢?你的代码在这种情况下的扩展性又会如何呢?
拓展声明:
“锯齿” 顺序对于 k > 2
的情况定义可能会有些歧义。所以,假如你觉得 “锯齿” 这个表述不妥,也可以认为这是一种 “循环”。例如:
输入:
[1,2,3]
[4,5,6,7]
[8,9]
输出: [1,4,8,2,5,9,3,6,7]
.
定义 vectors 列表保存输入的所有一维向量,indexes 表示 vectors 列表每一项当前所遍历到的下标位置,cur 表示当前遍历到的 vector 列表,而 size 表示 vectors 列表元素个数。具体实现参考以下代码实现。
class ZigzagIterator:
def __init__(self, v1: List[int], v2: List[int]):
self.cur = 0
self.size = 2
self.indexes = [0] * self.size
self.vectors = [v1, v2]
def next(self) -> int:
vector = self.vectors[self.cur]
index = self.indexes[self.cur]
res = vector[index]
self.indexes[self.cur] = index + 1
self.cur = (self.cur + 1) % self.size
return res
def hasNext(self) -> bool:
start = self.cur
while self.indexes[self.cur] == len(self.vectors[self.cur]):
self.cur = (self.cur + 1) % self.size
if self.cur == start:
return False
return True
# Your ZigzagIterator object will be instantiated and called as such:
# i, v = ZigzagIterator(v1, v2), []
# while i.hasNext(): v.append(i.next())
public class ZigzagIterator {
private int cur;
private int size;
private List<Integer> indexes = new ArrayList<>();
private List<List<Integer>> vectors = new ArrayList<>();
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
cur = 0;
size = 2;
indexes.add(0);
indexes.add(0);
vectors.add(v1);
vectors.add(v2);
}
public int next() {
List<Integer> vector = vectors.get(cur);
int index = indexes.get(cur);
int res = vector.get(index);
indexes.set(cur, index + 1);
cur = (cur + 1) % size;
return res;
}
public boolean hasNext() {
int start = cur;
while (indexes.get(cur) == vectors.get(cur).size()) {
cur = (cur + 1) % size;
if (start == cur) {
return false;
}
}
return true;
}
}
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/