Problem:

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?

Analysis:

This kind of problem is very easy! But you need try hold some good pinciple in design and implementation, it could greatly improve your coding ability. 

When you meet the problem of implementing a specific iterator, the first thing is not try to come up with your own totaly innovative iterator.
Like: list.get(i) (and control over i)
Even that way is possible, but you have to tackle may possible cases, which is hard to be right!!! However, you should take advantage of existing iterator of arguments!!! Which have already implemented a delicate iterator, with common actions: hasNext(), next(). Define your invariant:
For this problem, we want our "cur_iterator" always point to right position, thus we could directly use underlying "iterator1.hasNext()" or "iterator2.hasNext()". And once we make sure next element is existed, we could use "cur_iterator.next()" to get the right answer back.
Since we need to have a special overall iterator for the problem, and this iterator actually takes advantage of other iterators together, our goal is to find the right mechanims to "use underlying iterators to acheive the effect that the overall iterator displays". Step 1: Initialize overall iterator.
this.cur_iterator = (this.iterator1.hasNext() ? this.iterator1 : this.iterator2);
Note: iff there are unscanned elements in lists, we must guarantee next() function could reach them.
<You must consider the case of l1: [], l2: [1, 2, 3]> And the luckily thing is we don't need to test l1's length, directly use l1.hasNext() could elegantly achieve this purpose. Step 2: Adjust the iterator when we finish the scan of an element.
The core part for "ZigZag operation".
Key: point to the other iterator when other iterator still has unscanned elements. Otherwise, remain on the same list.
if (cur_iterator == iterator1) {
if (iterator2.hasNext()) {
cur_iterator = iterator2;
}
}

Solution:

public class ZigzagIterator {
Iterator<Integer> cur_iterator;
Iterator<Integer> iterator1;
Iterator<Integer> iterator2; public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
this.iterator1 = v1.iterator();
this.iterator2 = v2.iterator();
this.cur_iterator = (this.iterator1.hasNext() ? this.iterator1 : this.iterator2);
} public int next() {
int ret = cur_iterator.next();
if (cur_iterator == iterator1) {
if (iterator2.hasNext()) {
cur_iterator = iterator2;
}
} else{
if (iterator1.hasNext()) {
cur_iterator = iterator1;
}
}
return ret;
} public boolean hasNext() {
return cur_iterator.hasNext();
}
}

[LeetCode#281] Zigzag Iterator的更多相关文章

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

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

  2. 281. Zigzag Iterator

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

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

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

  4. 281. Zigzag Iterator z字型遍历

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

  5. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

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

  6. [Locked] Zigzag Iterator

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

  7. Zigzag Iterator II

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

  8. [LeetCode] 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. 关于dialog的一点知识

    一般我们比较常用的就是AlertDialog,这个一般也不直接构造,而是用系统提供的builder构造器去构造一个dialog. AlertDialog.Builder builder = new A ...

  2. 记一次ios使用OAuth 2.0写的接口获取token的小错

    1.用ios原生网络请求的话,请求参数不能这样传 而要这样传 2.用afnetworking的话,要注意各个参数有没有错误,参数可以直接这样传

  3. java新手笔记12 单例

    1.单例 package com.yfs.javase; public class Singleton { //private static final Singleton single = new ...

  4. javaee学习-JSP指令简介

    JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: page指令 Inclu ...

  5. dp方程

    1.        资源问题1 -----机器分配问题 F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2 ------01背包问题   F[I,j]:=ma ...

  6. ASP.NET设计模式(一)、适配器模式、依赖注入依赖倒置、空对象模式

    鸟随凤鸾,人伴贤良,得以共之,我之幸也.说的是鸟随着鸾凤可以飞的更高远,人和比自己境界高的相处,自己也会得到熏染进步. 一.概述 分享出来简单的心得,望探讨 依赖倒置 依赖注入 Adapter模式 N ...

  7. ubuntu zendDebugger.so 加载不上的问题

    参考文章   http://blog.sina.com.cn/s/blog_6612d5810101dapf.html 装zenDdebugger是为了在eclipse中调试用!!!!!!!结果搞了半 ...

  8. 网站开发常用jQuery插件总结(五)滚动条插件nanoscroller

    网站在展示信息时,如果信息量过大,解决方法主要有三种.1.分页,将信息分页显示.2.整页显示,但是页面过长,影响浏览体验.3.使用滚动条,而默认滚动条样式太单一,用户体验不友好.所以我们需要美化滚动条 ...

  9. 测试网站是共享还是独立ip

    查看是共享还是独立:http://www.yougetsignal.com/tools/web-sites-on-web-server/ 站长工具:http://tool.webmasterhome. ...

  10. javascript进阶——面向对象特性

    面向对象的javascript是这门语言被设计出来时就考虑的问题,熟悉OOP编程的概念后,学习不同的语言都会发现不同语言的实现是不同的,javascript的面向对象特性与其他具有面向对象特性的语言的 ...