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. Docker Dockerfile COPY vs ADD

    http://blog.163.com/digoal@126/blog/static/163877040201410341236664/   在Dockerfile中, 我们可以使用ADD和COPY拷 ...

  2. IdentityDbContext

    Move the ApplicationUser definition to your DAL. Inherit your MyDbContext from IdentityDbContext< ...

  3. queryString(正则表达式版本)

    获取所有query string function queryStringAll(s) { var reg = /(?:^|&)([^&]+)=([^&]+)(?=&| ...

  4. Orchard源码分析(4.4):Orchard.Caching.CacheModule类

    概述 CacheModule也是一个Autofac模块.   一.CacheModule类 CacheModule将DefaultCacheManager注册为ICacheManager:       ...

  5. 关于VS打开cshtml出现 未能完成该操作。无效指针

    关于VS打开cshtml出现 未能完成该操作.无效指针 第一步:关闭VS 第二部:删除%LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModel ...

  6. php简单实用的操作文件工具类(创建、移动、复制、删除)

    php简单实用好用的文件及文件夹复制函数和工具类(创建.移动.复制.删除) function recurse_copy($src,$dst) {  // 原目录,复制到的目录 $dir = opend ...

  7. git 初始化

    Git global setup git config --global user.name "杨清1" git config --global user.email " ...

  8. HDOJ 3709 Balanced Number

    数位DP... Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java ...

  9. Ubuntu 12 安装 MySQL 5.6.26 及 问题汇总

    参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: #安装依赖库 sudo apt-get install ...

  10. Swift3.0P1 语法指南——基本操作符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...