Description

Follow up Zigzag Iterator: What if you are given k 1d vectors? How well can your code be extended to such cases? The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic".

Example

Example1

Input: k = 3
vecs = [
[1,2,3],
[4,5,6,7],
[8,9],
]
Output: [1,4,8,2,5,9,3,6,7]

Example2

Input: k = 3
vecs = [
[1,1,1]
[2,2,2]
[3,3,3]
]
Output: [1,2,3,1,2,3,1,2,3]

思路:

用k个指针维护每个数组的当前最靠前的元素,循环枚举这k个指针,每次枚举到就在答案序列中将该数加入,如果指针到了该数组的尾部,那么就不作处理

 
public class ZigzagIterator2 {

    public List<Iterator<Integer>> its;
public int turns; /**
* @param vecs a list of 1d vectors
*/
public ZigzagIterator2(List<List<Integer>> vecs) {
// initialize your data structure here.
this.its = new ArrayList<Iterator<Integer>>();
for (List<Integer> vec : vecs) {
if (vec.size() > 0)
its.add(vec.iterator());
}
turns = 0;
} public int next() {
// Write your code here
int elem = its.get(turns).next();
if (its.get(turns).hasNext())
turns = (turns + 1) % its.size();
else {
its.remove(turns);
if (its.size() > 0)
turns %= its.size();
}
return elem;
} public boolean hasNext() {
// Write your code here
return its.size() > 0;
}
}
/**
* Your ZigzagIterator2 object will be instantiated and called as such:
* ZigzagIterator2 solution = new ZigzagIterator2(vecs);
* while (solution.hasNext()) result.add(solution.next());
* Output result
*/

  

Zigzag Iterator II的更多相关文章

  1. 281. Zigzag Iterator

    题目: Given two 1d vectors, implement an iterator to return their elements alternately. For example, g ...

  2. [Locked] Zigzag Iterator

    Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...

  3. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  4. Zigzag Iterator

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  5. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...

  6. [LeetCode#281] Zigzag Iterator

    Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

  7. [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  8. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  9. [LeetCode] 281. Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...

随机推荐

  1. golang socket与Linux socket比较分析

    在posix标准推出后,socket在各大主流OS平台上都得到了很好的支持.而Golang是自带runtime的跨平台编程语言,Go中提供给开发者的socket API是建立在操作系统原生socket ...

  2. NP完全问题的证明

    目录 NP完全问题的证明 一.限制法 最小覆盖问题(VC) 子图同构问题 0-1背包(Knapsack) 三元集合的恰当覆盖(X3C) 集中集 有界度的生成树 多处理机调度 二.局部替换法 3SAT问 ...

  3. Win10 UEFI 系统安装教程

    1:首先我们需要先拿一个U盘,制作一个带UEFI PE.(网上的大白菜以及老毛桃都可以,我用的是从网上找的WIN10 PE.WIN10 PE的好处是集成了NVME驱动,可以认得到SM951 NVME版 ...

  4. Cglib 与 JDK动态代理

    作者:xiaolyuh 时间:2019/09/20 09:58 AOP 代理的两种实现: jdk是代理接口,私有方法必然不会存在在接口里,所以就不会被拦截到: cglib是子类,private的方法照 ...

  5. cygwin中修改path变量

    1.在家目录建立 .bash_profile 文件. 2.在该文件添加: export PATH=/my/path/:$PATH 3.解释,/my/path/为你要添加的目录,为什么不在.bashrc ...

  6. 【转】Unobtrusive Ajax的使用

    [转]Unobtrusive Ajax的使用 Ajax (Asynchronous JavaScript and XML 的缩写),如我们所见,这个概念的重点已经不再是XML部分,而是 Asynchr ...

  7. python基础03--int,bool,str

    1.1 数字int 1.i = 100  i.bit_length()   转化为二进制的最小位数 1.2 布尔 bool 1.True  False      0是False 1.3 数据转换    ...

  8. Python列表,元组,字典,集合

    列表 Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能. 列表追加数据的方法:append(),extend(数组),insert(位 ...

  9. Solr基础知识三(整合SSM)

    前两篇讲了solr安装和导入数据,这篇讲如何整合到SSM中. 一.整合SSM 1.1 引入依赖 1.2 初始化solr 1.3 写service 1.4 写控制层 1.5 查询 二.IK分词器 2.1 ...

  10. python 读写excel(xls格式)

    import xlrd #导入模块 from xlutils.copy import copy #导入copy模块 rb = xlrd.open_workbook(r"e:\额度导入表.xl ...