Zigzag Iterator ii
来源:互联网 发布:下载输入法软件 编辑:程序博客网 时间:2024/05/23 02:00
Follow up Zigzag Iterator: What if you are given k
1d vectors? How well can your code be extended to such cases? 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".
Example
Given k = 3
1d vectors:
[1,2,3][4,5,6,7][8,9]
Return [1,4,8,2,5,9,3,6,7]
.
java
public class ZigzagIterator2 { /* * @param vecs: a list of 1d vectors */ List<Iterator<Integer>> list; int count; public ZigzagIterator2(List<List<Integer>> vecs) { // do intialization if necessary count = 0; list = new ArrayList<Iterator<Integer>>(); for (List<Integer> li : vecs) { if (!li.isEmpty()) { list.add(li.iterator()); } } } /* * @return: An integer */ public int next() { // write your code here int ele = list.get(count).next(); if (list.get(count).hasNext()) { count = (count + 1) % list.size(); } else { list.remove(count); if (!list.isEmpty()) { count %= list.size(); } } return ele; } /* * @return: True if has next */ public boolean hasNext() { // write your code here return list.size() > 0; }}/** * Your ZigzagIterator2 object will be instantiated and called as such: * ZigzagIterator2 solution = new ZigzagIterator2(vecs); * while (solution.hasNext()) result.add(solution.next()); * Output result */
阅读全文
0 0
- #541 Zigzag Iterator II
- Zigzag Iterator ii
- Zigzag Iterator ii
- Zigzag Iterator
- Zigzag Iterator
- Zigzag Iterator
- Zigzag Iterator
- Leetcode: Zigzag Iterator
- leetcode 281: Zigzag Iterator
- *LeetCode-Zigzag Iterator
- [LeetCode281]Zigzag Iterator
- [leetcode281]Zigzag Iterator
- LeetCode 281. Zigzag Iterator
- Leetcode Zigzag Iterator
- #540 Zigzag Iterator
- LeetCode 271. Zigzag Iterator
- 281. Zigzag Iterator
- LeetCode 题解(272) : Zigzag Iterator
- [LeetCode]64. Minimum Path Sum
- 2017年10月23日提高组T2 灵知的太阳信仰
- CentOS下安装配置LAMP(Linux+Apache+MySQL+PHP)
- GYM
- Oracle数据库的union与intersect
- Zigzag Iterator ii
- Java内存分配详解(堆内存、栈内存、常量池)
- NOJ(1575)-回溯算法-图的m着色问题
- bzoj 2438: [中山市选2011]杀人游戏 强连通分量
- HashMap和ConcurrentHashMap比较
- 配置Kerberos+LDAP整合,共用LDAP 数据库
- centos基于ip的虚拟主机
- vue.js组件的重要选项
- Python学习笔记(一):基本语法