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. poi根据excel模板导出Excel

    /****单元格值对象**/public class Cells { /*** * 行 */ private int row; /** * 列 */ private int column; /** * ...

  2. Redis Desktop Manager 0.9.3 版本下载

    因为Redis Desktop Manager作者在 0.9.4 版本之后选择对所有的安装包收费,不再提供安装包下载,但是源码依旧公开.链接:https://pan.baidu.com/s/1SXsy ...

  3. 螺旋折线-C++

    标题:螺旋折线 如图p1.png所示的螺旋折线经过平面上所有整点恰好一次. 对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. 例如dis(0, ...

  4. Oracle学习笔记(五)

    如何查询硬解析问题: --捕获出需要使用绑定变量的SQL drop table t_bind_sql purge; create table t_bind_sql as select sql_text ...

  5. C#中精确计时的一点收获 Stopwatch

    http://www.cnblogs.com/jintianhu/archive/2010/09/01/1815031.html 参考: https://www.cnblogs.com/kissdod ...

  6. C# vb .net图像合成-合成椭圆

    在.net中,如何简单快捷地实现图像合成呢,比如合成文字,合成艺术字,多张图片叠加合成等等?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码 ...

  7. 2019 吉比特java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.吉比特等公司offer,岗位是Java后端开发,因为发展原因最终选择去了吉比特,入职一年时间了,也成为了面试官 ...

  8. CodeForces 536D Tavas in Kansas

    洛谷题目页面传送门 & CodeForces题目页面传送门 A和B在一张无向连通图\(G=(V,E),|V|=n,|E|=m\)上玩一个游戏,节点\(i\)有一个权值\(v_i\).A.B分别 ...

  9. Java自学-类和对象 类方法

    Java的类方法和对象方法 类方法: 又叫做静态方法 对象方法: 又叫实例方法,非静态方法 访问一个对象方法,必须建立在有一个对象的前提的基础上 访问类方法,不需要对象的存在,直接就访问 步骤 1 : ...

  10. 前端1-----A标签 (锚点)

    Title 头部,顶端, 点击跳转到id='abc' 1 2 到百度这里,点击跳转百度 3 到这里了 4 alex1 5 6 根据id 从底部到头      根据id='a1' 到指定位置      ...