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

 public class ZigzagIterator {

     List<Iterator<Integer>> iters = new ArrayList<>();
int count = ; public ZigzagIterator(List<List<Integer>> lists) {
for (List<Integer> v : lists) {
if (!v.isEmpty()) iters.add(v.iterator());
}
} public int next() {
int x = iters.get(count).next();
if (!iters.get(count).hasNext())
iters.remove(count);
else
count++; if (iters.size() != )
count %= iters.size();
return x;
} public boolean hasNext() {
return !iters.isEmpty();
}
} /**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2); while (i.hasNext()) v[f()] =
* i.next();
*/

Zigzag Iterator的更多相关文章

  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. Zigzag Iterator II

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

  4. [LeetCode] 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. VS插件开发,启用实验室环境

    启用外部程序: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe 命令行参数 /rootsuffix ...

  2. Linux下显示ip所属位置

    在linux下,要是网络出现延迟,通常我们需要分析自己到对端的服务器的网络环境 例:ping www.baidu.com traceroute www.baidu.com 通过分析来确定大概是什么问题 ...

  3. 注入问题0x00

    1.sqlmap遇到MySQL注入可以成功getshell,但是,遇到sqlserver注入未成功getshell. 2.xp_cmdshell 如何 getshell(1433未对外开放). 解决方 ...

  4. Yii2 rules验证规则

    Rules验证规则:  required : 必须值验证属性||CRequiredValidator 的别名, 确保了特性不为空. [['字段名1','字段名2'],required]    //字段 ...

  5. Mac终端Terminal调用Sublime Text

    Sublime Text 本身提供了命令行工具, 只需要在 Terminal 中输入以下内容就行了 sudo ln -s /Applications/Sublime\ Text.app/Content ...

  6. MYTOP安装和使用

    安装 mytop 1. 在 /etc/yum.repo.d 新建一个文件 21andy.com.repo [21Andy.com] name=21Andy.com Packages for Enter ...

  7. python sys os hashlib_MD5 模块

    模块 内置模块是Python自带的功能,在使用内置模块相应的功能时,需要[先导入]再[使用] 一.sys 用于提供对Python解释器相关的操作: ? 1 2 3 4 5 6 7 8 9 sys.ar ...

  8. java 递归获取一个目录下的所有文件路径

    还是日志的问题,log4j生成的日志文件,自动保存到月份所在的文件夹中,需要获取到所有的日志文件,包括文件夹 private List<String> ergodic(File file, ...

  9. Cucumber

    http://www.ibm.com/developerworks/library/a-automating-ria/ Cucumber is a testing framework that hel ...

  10. linux访问windows共享文件夹的方法

    博客转自:http://www.01happy.com/linux-access-windows-shares-folders/ 有时需要在linux下需要访问windows的共享文件夹,可以使用mo ...