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. 1082 射击比赛 PAT (Basic Level)

    题目链接: https://pintia.cn/problem-sets/994805260223102976/problems/994805260990660608 分析: 这是典型的给出编号和得分 ...

  2. Java学习:Map接口

    Map集合 Collection接口: 定义了单列集合规范 Collection<E> 每次存储一个元素 单个元素 Map接口:定义了双列集合的规范 Map<K,V> 每次存储 ...

  3. What Is HLS (HTTP Live Streaming)?

    HTTP Live Streaming  (HLS) Executive Summary HTTP Live Streaming (or HLS) is an adaptive streaming c ...

  4. LaTeX转义特殊符号

    转义字符在LaTeX中有一些符号被用于特殊的用途,如 \\      \backslash\ 符号被用于命令的转义,直接在LaTeX中输入这些符号是无法正确得到这些符号的,甚至会引起LaTeX的报错. ...

  5. laravel5.4 中 dd和dump的区别。

    在laravel中dd和dump 都是打印的数据的,但是 dd会终止程序的运行,dump不会. 而且dump打印出来的数据在浏览器上是高亮的哦(很有逼格的~). 上图为dump打印出来的. 上图为dd ...

  6. 【转】2019年7月份,阿里最新Java高频面试真题汇总

    技术一面(23问)技术二面(3大块)JAVA开发技术面试中可能问到的问题(17问)JAVA方向技术考察点(33快)项目实战(7大块)必会知识(48点)面试小技巧注意事项1. 阿里技术一面 Java I ...

  7. linuxmint安装Tools找不到Tools的压缩包问题

    安装Linuxmint之后按照惯例安装Tools,打开桌面上的Tools光盘之后找不到压缩包. PS:因为已经装好了,就不上图了,按照下面的步骤做就没有问题了. 1:找到vmware的安装目录下的li ...

  8. statsvn使用小记

    准备工作 1.安装TortoiseSVN,在安装时需要安装svn命令行工具: 2.拉取svn代码: svn co https://svn.myserver.cn/svn/myproject1\trun ...

  9. Java 之 Object 类

    一.Object 概述  java.lang.Object 类是 Java 语言中的根类,即所有类的父类. 在对象实例化的时候,最终找的父类就是 Object. 如果一个类没有特别指定父类,那么默认则 ...

  10. FreePascal - CodeTyphon 和 Lazarus, 如何像Delphi一样有代码之间的连线?

    CodeTyphon 6.9 默认CodeTyphon没有开启代码之间连线功能,一直不知道如何调出来,在使用CodeTyphon 和 Lazarus 很渴望也能像Delphi那样有这种功能,实际上它们 ...