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. JS获取日期和时间

    //获取日期和时间 function showDate(){ var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFul ...

  2. get share button count

    class shareCount { private $url,$timeout; function __construct($url,$timeout=10) { $this->url=raw ...

  3. jquery禁用a标签,jquery禁用按钮click点击

    jquery禁用a标签方法1 $(document).ready(function () { $("a").each(function () { var textValue = $ ...

  4. linux系统文件的颜色代表的意思

    1.蓝色代表目录 2.绿色代表可执行文件 3.红色代表可压缩文件 4.白色代表其他文件 5.浅蓝色代表链接文件 6.黄色代码设备 7.红色闪烁表示链接的文件有问题

  5. android中listview的item滑动删除效果(已解决listview点击问题)

    领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面 ...

  6. [記錄用]python py2app 檔案批次重新命名

    demo.py 主要作用為 將同目錄下 *.mp4 檔案批次重新命名 例如: aaa001.mp4 ---重新命名為--> 001.mp4 aaa002.mp4 ---重新命名為--> 0 ...

  7. 快速读取csv平面文件,并导入数据库,简单小工具

    using DataToDeal; using LumenWorks.Framework.IO.Csv; using Microsoft.Win32; using System; using Syst ...

  8. 服务器慢 mysql-bin.000001文件占满磁盘的原因与解决

    发现 VPS 服务器上的网站反应超级慢,简单的重启.重启各主要服务,发现mysql 的反应极其不正常. 一方面是问题,这与站点访问量有关.开始时从mysql 的配置文件 my.cnf 考虑,但志文工作 ...

  9. VC菜菜鸟:建立第一个基于Visual C++的Windows窗口程序

    建立第一个基于VisualC++的Windows窗口程序: 发表于:http://blog.csdn.net/it1988888/article/details/10306585 a)执行命令:新建 ...

  10. QT窗口渐现效果,窗口震动效果,鼠标移动窗口

    //窗口渐现效果void MainWindow::closeWindowAnimation() //关闭窗口效果 { QPropertyAnimation *animation = new QProp ...