给出两个一维的向量,请你实现一个迭代器,交替返回它们中间的元素。
示例:
输入: 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]
.
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();
*/
struct ZigzagIterator {
v1: Vec<i32>,
v2: Vec<i32>,
/// `false` represents `v1`, `true` represents `v2`
flag: bool,
}
impl ZigzagIterator {
fn new(v1: Vec<i32>, v2: Vec<i32>) -> Self {
Self {
v1,
v2,
// Initially beginning with `v1`
flag: false,
}
}
fn next(&mut self) -> i32 {
if !self.flag {
// v1
if self.v1.is_empty() && !self.v2.is_empty() {
self.flag = true;
let ret = self.v2.remove(0);
return ret;
}
if self.v2.is_empty() {
let ret = self.v1.remove(0);
return ret;
}
let ret = self.v1.remove(0);
self.flag = true;
return ret;
} else {
// v2
if self.v2.is_empty() && !self.v1.is_empty() {
self.flag = false;
let ret = self.v1.remove(0);
return ret;
}
if self.v1.is_empty() {
let ret = self.v2.remove(0);
return ret;
}
let ret = self.v2.remove(0);
self.flag = false;
return ret;
}
}
fn has_next(&self) -> bool {
!self.v1.is_empty() || !self.v2.is_empty()
}
}