题目:

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].

链接: http://leetcode.com/problems/zigzag-iterator/

题解:

Zigzag Iterator。最简单的就是用两个index分别遍历两个list,然后用一个boolean变量来控制何时遍历哪一个list。 好像我这么做是有问题的,应该用Interator<>来做。

Time Complexity - O(n), Space Complexity - O(1)

public class ZigzagIterator {
private int length;
private int index1 = 0;
private int index2 = 0;
private List<Integer> list1;
private List<Integer> list2;
private boolean useList2; public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
this.length = v1.size() + v2.size();
list1 = v1;
list2 = v2;
if(list1.size() == 0) {
useList2 = true;
}
} public int next() {
if(!useList2) {
if(index2 < list2.size()) {
useList2 = true;
}
return list1.get(index1++);
} else {
if(index1 < list1.size()) {
useList2 = false;
}
return list2.get(index2++);
} } public boolean hasNext() {
return (index1 + index2) < length;
}
} /**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/

Reference:

https://leetcode.com/discuss/57961/o-n-time-%26-o-1-space-java-solution

https://leetcode.com/discuss/63037/simple-java-solution-for-k-vector

https://leetcode.com/discuss/58012/short-java-o-1-space

https://leetcode.com/discuss/71857/clean-java-solution-works-for-k-lists

281. Zigzag Iterator的更多相关文章

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

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

  2. [LeetCode#281] Zigzag Iterator

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

  3. 281. Zigzag Iterator z字型遍历

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

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

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

  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] Zigzag Iterator 之字形迭代器

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

  8. Zigzag Iterator

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

  9. LeetCode Zigzag Iterator

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

随机推荐

  1. How to compile and install NCAR Command Language on IBM PowerPC 64 --- NCL编译安装步骤

    作者:Sinsonglew 出处:http://www.cnblogs.com/sinsonglew 欢迎转载,也请保留这段声明.thanks :) 注记:NCL官方依赖安装包全集列表.官方源码编译指 ...

  2. tcp传输黏包

    tcp传输黏包 tcpip协议使用"流式"(套接字)进行数据的传输,就是说它保证数据的可达以及数据抵达的顺序,但并不保证数据是否在你接收的时候就到达,特别是为了提高效率,充分利用带 ...

  3. 软件工程实践小队Scrum Meeting

    例会记录 时间:2013年10月20日星期日 Part 1 会议要点: 1. 小组讨论PM.Dev.Test的各自特点: 2. 小组讨论我们的项目网上教学问答系统的相关问题: 3.确定小组成员在第一轮 ...

  4. hive搭建配置

    下载cd /data0/software/hivewget http://mirror.bit.edu.cn/apache/hive/hive-0.12.0/hive-0.12.0-bin.tar.g ...

  5. Bootstrap入门四:代码

    1.内联代码 code: 通过 <code> 标签包裹内联样式的代码片段.灰色背景.灰色边框和红色字体. For example, <code><section>& ...

  6. Bootstrap入门三:页面排版

    在Bootstrap中,页面的排版都是从全局的概念上出发,定制了主体文本.强调文本.标题.Code风格.按钮.表单.表格等格式,并运用CSS3的@font-face和伪元素一起实现了一套icon主题. ...

  7. 【Sum Root to Leaf Numbers】cpp

    题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...

  8. Ubuntu 16.04 下卸载 lnmp/lamp 方法

    1.卸载 apache2 sudo apt-get --purge remove apache2* sudo apt-get autoremove apache2 (--purge 是完全删除并且不保 ...

  9. JS模板引擎 :ArtTemplate (2)

    上一篇初略的介绍了一下javascript中的模板引擎,有兴趣的可以戳 这里 . 这一篇将带着大家一起做一个简易的模板引擎, 上一篇介绍到:模板引擎其实做的就是两件事. 根据一定的规则,解析我们所定义 ...

  10. php正则过滤html标签、空格、换行符的代码,提取图片

    $descclear = str_replace("r","",$descclear);//过滤换行 $descclear = str_replace(&quo ...