LeetCode Zigzag Iterator
原题链接在这里:https://leetcode.com/problems/zigzag-iterator/
题目:
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?
Clarification for the follow up question - Update (2015-09-18):
The "Zigzag" order is not clearly defined and is ambiguous for k > 2 cases. If "Zigzag" does not look right to you, replace "Zigzag" with "Cyclic". For example, given the following input:
[1,2,3]
[4,5,6,7]
[8,9]
It should return [1,4,8,2,5,9,3,6,7].
题解:
用queue来保存每个list的iterator. next() 是 poll 出que的第一个iterator, return iterator.next(), 若是该iterator还有next, 就再放到queue尾部.
若queue空了,说明都遍历过了,此时hasNext()就返回false.
Time Complexity: next, O(1). hasNext, O(1). Space: O(l), l 是list的个数.
AC Java:
public class ZigzagIterator {
LinkedList<Iterator<Integer>> que;
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
que = new LinkedList<Iterator<Integer>>();
if(v1.iterator().hasNext()){
que.add(v1.iterator());
}
if(v2.iterator().hasNext()){
que.add(v2.iterator());
}
}
public int next() {
Iterator<Integer> it = que.poll();
int cur = it.next();
if(it.hasNext()){
que.add(it);
}
return cur;
}
public boolean hasNext() {
return !que.isEmpty();
}
}
/**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/
LeetCode Zigzag Iterator的更多相关文章
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- [LeetCode] Peeking Iterator 顶端迭代器
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 281. Zigzag Iterator
题目: Given two 1d vectors, implement an iterator to return their elements alternately. For example, g ...
- [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] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
随机推荐
- [Noi2015]软件包管理器 题解
题目大意: 有n个软件安装包,除第一个以外,其他的要在另一个安装包的基础上安装,且无环,问在安装和卸载某个软件包时,这个操作实际上会改变多少个软件包的安装状态. 思路: 可构成树,用树链剖分,线段树. ...
- [BZOJ2794][Poi2012]Cloakroom
2794: [Poi2012]Cloakroom Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 167 Solved: 119[Submit][St ...
- BZOJ3625: [Codeforces Round #250]小朋友和二叉树
Description 我们的小朋友很喜欢计算机科学,而且尤其喜欢二叉树.考虑一个含有n个互异正整数的序列c[1],c[2],...,c[n].如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合{ ...
- select..in(参数化) 解决注入式问题
方案1 为where in的每一个参数生成一个参数,写法上比较麻烦些,传输的参数个数有限制,最多2100个,可以根据需要使用此方案 using (SqlConnection conn = new Sq ...
- Eclipse安装nodeclipse插件
1. Start Eclipse, then select Help > Install New Software... 2. Enter the update site URL into th ...
- butterknife简介及Generate ButterKnife Injections 不出现的问题解决
一.概述 butterknife是一款as的功能强大插件.有了它,你几乎可以和findViewById说byebye了. 二.使用 github地址:https://github.com/avast/ ...
- ssh问题
一.基于秘钥认证: ssh-keygen ssh-copy-id -i .ssh/id_rsa.pub 10.10.10.70#在对方authorized_keys 文件中写入自己的id_rsa.pu ...
- JAVA创建并写入内容到xlsx文件
首先需要在web项目中导入jxl.jar 包 //action中代码 public String downloadReport(){ String path = System.getPr ...
- Daily Scrum 11.2
由于11月1号是周六,小组里人不是很齐,所以Scrum会议暂停一次. 周日大家的工作都已经进入到尾声了,但是由于人员方面出现一些问题,界面方面做的还不到位.鉴于我们还只是完成了一个比较简单的工作,与真 ...
- jQuery 插件autocomplete
jQuery 插件autocomplete 自动加载 参考: http://www.cnblogs.com/Peter-Zhang/archive/2011/10/22/2221147.html ht ...