[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 ...
随机推荐
- Js中的appenChild,insertBefore--createDocumentFragment
平时项目中会有一些流程,或者是评论相关的东西,这些一般只会是在页面初次加载一部分,剩余部分搞一个更多的标签,当点击更多的时候,ajax请求把所有数据加载完(当然这里也有分页的实现方法,本篇不作讨论), ...
- lucene 4.4 demo
ackage com.zxf.demo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStr ...
- oc ios 中文字符串 进行 sha1加密 错误?
我在网上找到了一个oc版加密的工具类,但是加密中文就出现大问题 const char *cstr = [self cStringUsingEncoding:encoding]; NSData *dat ...
- PHP pear安装出现 Warning: require_once(Structures/Graph.php)...错误
今天在WINDOWS安装pear,一路无阻很顺利安装完成,接着想安装下pear email包来玩下,但接下来却报: Warning: require_once(Structures/Graph.php ...
- 08_使用TCP/IP Monitor监视SOAP协议
[SOAP定义] SOAP 简单对象访问协议,基于http传输xml数据,soap协议体是xml格式.SOAP 是一种网络通信协议SOAP 即Simple Object Access Pr ...
- PHP CURL参数详解
curl用法:cookie及post 一.cookie用法 <?php $cookie_jar = tempnam('./tmp','cookie'); // login $c=curl_ini ...
- mysql复制表数据或表结构到新表中
MySQL复制表数据到新表的几个步骤. 1.MySQL复制表结构及数据到新表 CREATE TABLE new_table SELECT * FROM old_table; 2.只复制表结构到新表 C ...
- 系统监控的一些工具w , vmstat
w 命令:--w 查看的是系统整体上的负载 # w 15:23:46 up 3:34, 2 users, load average: 0.03, 0.05, 0.00 USER TTY FROM LO ...
- hadoop2.610集群配置(包含HA和Hbase )
.修改Linux主机名2.修改IP3.修改主机名和IP的映射关系######注意######如果你们公司是租用的服务器或是使用的云主机(如华为用主机.阿里云主机等)/etc/hosts里面要配置的是内 ...
- SORT_AREA_RETAINED_SIZE
manual pga: SORT_AREA_RETAINED_SIZE specifies (in bytes) the maximum amount of the user global area ...