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. ActionBar只显示图标不显示文字

    问题:ActionBar菜单项android:showAsAction设置为android:showAsAction="always|withText"或者android:show ...

  2. 完全备份ORACLE数据库 并在另一台电脑上恢复

    由于最近有oracle的项目,需要把数据库在另外一台电脑里面配置一个一样的数据库用来测试开发用,之前是一直使用mssql,只需要附加或者还原就行,但是在oracle里面,就没有这么简单,但是也不难,操 ...

  3. (六)Hibernate 映射类型

    所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:基本类型映射 例子: hibernate.cfg.xml < ...

  4. SDL实现按钮

    是的,按钮控件很常见,几乎在每一个Windows窗体内都能找到它的身影.SDL作为一套“一套开放源代码的跨平台多媒体开发库”,自然可以实现按钮.而按钮实现的重点,就是SDL的鼠标响应事件. SDL的鼠 ...

  5. 263. Ugly Number(C++)

    263. Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are ...

  6. Xcode 5.1 更新后插件不能用

    打开目录 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins 右击选择"显示包内容" 找到"Inf ...

  7. 使用CPA4破解经典密码算法

    下面是一段经过经典密码算法加密的密文(加密算法未知): yvvnerujjvnywhbdvkpchfgvjtzwqsuporqfzpoekkjgziicdwwkeejdsruef   whwseyej ...

  8. (转)Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage

    ,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata e ...

  9. 疯狂学习java web

    因工作需要,疯狂学习java web,只是这么多年一直从事C++开发,突然之间要接手同事的那么一大堆代码,真有无从下手的感觉,首先是要学习html,然后是js, 然后是jsp,当然还有各种框架,想想就 ...

  10. 开源搜索引擎Sphinx 中启动多个搜索进程的方法

    http://blog.163.com/yang_jianli/blog/static/1619900062010316504471/ 要在同一机器上启动多个sphinx搜索进程searchd,必须为 ...