[LeetCode#281] Zigzag Iterator
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的更多相关文章
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
- 281. Zigzag Iterator
题目: Given two 1d vectors, implement an iterator to return their elements alternately. For example, g ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [Locked] Zigzag Iterator
Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...
- Zigzag Iterator II
Description Follow up Zigzag Iterator: What if you are given k 1d vectors? How well can your code be ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- LeetCode Zigzag Iterator
原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...
随机推荐
- prototype原型链继承
依旧是恶补js基础,上代码: 1.定义父类及父类方法 function Animal(){ this.name = "动物"; } Animal.prototype.eat = f ...
- vi 或 vim 常用命令(简单够用了)
1.vi filename :打开或新建文件,并将光标置于第一行首 2.按下i键:编辑或插入数据3.按下shit+: ->表示可以进行命令输入 4.q! ->表示不保存退出.5.w -&g ...
- JS实现回到页面顶部动画效果 2016.03.23
最近在模仿各大网站写页面样式和交互,发现好多都有回到顶部的需要,所以写了一下js,记录下来. 发现还可以添加从快到慢的动画效果和随时下拉滚动条停止滚动的功能, 参考了imooc上相关课程,最终实现JS ...
- IOS 学习笔记 2015-03-18
Objective--C 一 关键字 1 KVC 动态设值,动态取值,类似雨java中的反射,而且私有的照样可以设置与获取 2 二 函数 1 retain 给对象引用计数器 + 1 2 release ...
- 设计模式之 Factory Method 工厂方法
看到的比较有意思的一篇描述工厂方法的文章. http://www.codeproject.com/Articles/492900/From-No-Factory-to-Factory-Method 总 ...
- jquery-ui-datepicker定制化,汉化,因手机布局美观化源码修改
感谢浏览,欢迎交流=.= 公司微信网页需要使用日历控件,想到jquery-mobile,但是css影响页面布局,放弃后使用jquery-ui-datepicker. 话不多说,进入正题: 1.jque ...
- std::string stringf(const char* format, ...)
std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 ...
- jQuery选择器(适合初学者哟....)
选择器是jQuery最基础的东西,本文中列举的选择器基本上囊括了所有的jQuery选择器,也许各位通过这篇文章能够加深对jQuery选择器的理解,它们本身用法就非常简单,我更希望的是它能够提升个人编写 ...
- (jQuery 插件)封装容器的表单为json对象
下面代码可以把一个页面容器中的表单元素封装成一个json对象. (function($){ $.fn.serializeObject=function(){ var inputs=$(this).fi ...
- Sql中Rank排名函数
A.对分区中的行进行排名 以下示例按照数量对指定清单位置的清单中的产品进行了排名. 结果集按 LocationID 分区并在逻辑上按 Quantity 排序. 注意,产品 494 和 495 具有相同 ...