原题链接在这里: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. Financial Management[POJ1004]

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 179458   Accepted: ...

  2. 福建红色文化VR/AR实体体验馆正式启用

    (12月13日),福建红色文化网上展示馆上线暨福建红色文化VR/AR实体体验馆启动仪式在福建省革命历史纪念馆举行.省委常委.宣传部长高翔出席仪式并宣布启动上线. 福建红色文化网上展示馆和VR/AR实体 ...

  3. OpenResty 简单编写一个Module

    使用 Lua module 来进行 Lua 代码的复用是推荐的做法.然后在用户代码中直接用require()来调用 module代码: local myTest = {} function myTes ...

  4. PHP递归小例子

    $news = M('productbase'); function digui($idd){ $child=M('navclass')->where('f_id='.$idd)->sel ...

  5. 在Eclipse中在线安装Emmet和图文使用教程

    ZenCoding 升级为 Emmet 之后,基于 Eclipse 的插件安装地址也发生了变化, 下面是在基于 Eclipse 的 IDE 中安装和使用 Emmet 的图文示例. 一.打开 Eclip ...

  6. javaweb之框架标签(day1 框架标签的使用)

    框架标签 <frameset> --rows:按照行进行划分<frameset rows='80,*'> --rows:按照列进行划分<frameset cols='80 ...

  7. webkit内核浏览器的CSS写法

    -webkit-tap-highlight-color: transparent; Mobile上点击链接高亮的时候设置颜色为透明 -webkit-user-select: none; 设置为无法选择 ...

  8. winform制作简单计算器

    public Form1() { InitializeComponent(); textBox2.Text = ";//主显示屏 textBox1.Text = "";/ ...

  9. html中标签的含义及作用

    链接:http://www.w3chtml.com/html/tag/div.html

  10. angular.js学习笔记之一

    angular也是一个MVC框架,其中M即model模型表示服务器,V即view视图代表html代码,C即control控制器用来处理用户交互的部分.