原题链接在这里:https://leetcode.com/problems/zigzag-iterator/

题目:

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

For example, given two 1d vectors:

v1 = [1, 2]
v2 = [3, 4, 5, 6]

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 3, 2, 4, 5, 6].

Follow up: What if you are given k 1d vectors? How well can your code be extended to such cases?

Clarification for the follow up question - Update (2015-09-18):
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". For example, given the following input:

[1,2,3]
[4,5,6,7]
[8,9]

It should return [1,4,8,2,5,9,3,6,7].

题解:

用queue来保存每个list的iterator. next() 是 poll 出que的第一个iterator, return iterator.next(), 若是该iterator还有next, 就再放到queue尾部.

若queue空了,说明都遍历过了,此时hasNext()就返回false.

Time Complexity: next, O(1). hasNext, O(1). Space: O(l), l 是list的个数.

AC Java:

 public class ZigzagIterator {
LinkedList<Iterator<Integer>> que;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
que = new LinkedList<Iterator<Integer>>();
if(v1.iterator().hasNext()){
que.add(v1.iterator());
}
if(v2.iterator().hasNext()){
que.add(v2.iterator());
}
} public int next() {
Iterator<Integer> it = que.poll();
int cur = it.next();
if(it.hasNext()){
que.add(it);
}
return cur;
} public boolean hasNext() {
return !que.isEmpty();
}
} /**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/

类似Flatten 2D Vector.

LeetCode Zigzag Iterator的更多相关文章

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

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

  2. [LeetCode] Peeking Iterator 顶端迭代器

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  3. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  4. 281. Zigzag Iterator

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

  5. [Locked] Zigzag Iterator

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

  6. Zigzag Iterator II

    Description Follow up Zigzag Iterator: What if you are given k 1d vectors? How well can your code be ...

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

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

  8. [LeetCode#281] Zigzag Iterator

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

  9. 【LeetCode】281. Zigzag Iterator 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...

随机推荐

  1. 20145304 刘钦令 Java程序设计第二周学习总结

    20145304 <Java程序设计>第2周学习总结 教材学习内容总结 java可区分基本类型和类类型(即参考类型)两大类型系统. 基本类型主要可区分为整数.字节.浮点数.字符与布尔. 整 ...

  2. 预定义指令之debug

    1)根据你必须知道的.NET一书, #define DEBUG class Program { static void Main(string[] args) { #if DEBUG Console. ...

  3. C#图片处理---基础

    简单的图片剪裁 using System; using System.Drawing; using System.Drawing.Drawing2D; using System.IO; namespa ...

  4. osg中使用MatrixTransform来实现模型的平移/旋转/缩放

    osg中使用MatrixTransform来实现模型的平移/旋转/缩放 转自:http://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html# ...

  5. jsp实现过滤器的简单例子

    web.xml配置如下: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=&quo ...

  6. Java log4j详细教程

    Java log4j详细教程 http://www.jb51.net/article/74475.htm

  7. SQL常用语句总结

    -------查询一个表有多少列select count(*) from sysobjects a join syscolumns bon a.id=b.idwhere a.name='XXX' -- ...

  8. 移位运算符(JAVA)

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>     :     右移运算符,num >&g ...

  9. 普通工程转为mvn工程

    不同类型的工程可以转为mvn工程, 只需要一个插件 You may need to install m2e-eclipse plugin in order to have this simple ut ...

  10. java类集框架图(google找的,备个份)