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 ...
随机推荐
- 2016年AR行业十大热点事件汇总
2016年即将接近尾声,增强现实在今年完成了里程碑式的跃进.无论是从新玩法的开发还是从大众接受度,以及行业巨头的青睐,无不证明这AR的无线潜力,故而,2016年算是AR的崛起之年. 纵观全年AR新闻事 ...
- CentOS6.4 访问域局网中Windows的共享
mount -t cifs -o username=" //10.10.3.246/f /usr/local/openresty/nginx/html/down 说明: mount -t c ...
- HDU 4750 Count The Pairs(并查集)
题目链接 没有发现那个点,无奈. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- 【Poj】 p2342 Anniversary party(树形DP第一道)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5523 Accepted: 3169 ...
- UITableView常见 UI 问题总结
一,经历 1.让 group 形式的UITableView的单元格也可以修改separatorStyle属性来设置. 2.修改group形式的UITableView的 cell 之间的间距,可以更改s ...
- docker 练习
echo 'DOCKER_OPTS="-b=bridge0"' >> /etc/default/docker sudo docker run -i -t centos ...
- 李洪强iOS经典面试题135-Objective-C
可能碰到的iOS笔试面试题(5)--Objective-C 面试笔试都是必考语法知识的.请认真复习和深入研究OC. Objective-C 方法和选择器有何不同?(Difference between ...
- [LintCode] Paint House II 粉刷房子之二
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- 如何给Sublime安装插件
第一步:点击链接http://sublime.wbond.net/Package%20Control.sublime-package下载Package Control. 第二步:点击打开Sublime ...
- HTML5 Input 类型
浏览器支持 Input type IE Firefox Opera Chrome Safari email No 4.0 9.0 10.0 No url No 4.0 9.0 10.0 No numb ...