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]
.
public class ZigzagIterator { List<Iterator<Integer>> iters = new ArrayList<>();
int count = ; public ZigzagIterator(List<List<Integer>> lists) {
for (List<Integer> v : lists) {
if (!v.isEmpty()) iters.add(v.iterator());
}
} public int next() {
int x = iters.get(count).next();
if (!iters.get(count).hasNext())
iters.remove(count);
else
count++; if (iters.size() != )
count %= iters.size();
return x;
} public boolean hasNext() {
return !iters.isEmpty();
}
} /**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2); while (i.hasNext()) v[f()] =
* i.next();
*/
Zigzag Iterator的更多相关文章
- 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] 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 ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
随机推荐
- 通过LVS+Keepalived搭建高可用的负载均衡集群系统
1. 安装LVS软件 (1)安装前准备操作系统:统一采用Centos6.5版本,地址规划如下: 服务器名 IP地址 网关 虚拟设备名 虚拟ip Director Server 192.168 ...
- php self与static的区别
self vs static 用一个demo来直接说明self与static的区别.self示例: <?phpclass Vehicle { protected static $name ...
- Last-Modify和Etag
Last-Modified和Etag Last-Modified是页面文件最后的修改时间,Etag相当于页面文件的hash. http request http respose If ...
- jqxTreeGrid
基本TreeGrid样本 <!DOCTYPE html> <html lang="en"> <head> <title id=" ...
- php开发工具之火狐浏览器插件
相信做开发的都有一种火狐情怀吧! 下面来介绍下一些自己在php开发工程中用到几个火狐浏览器插件. 1.[firebug]: 这个插件可以说是一个神奇,功能不用过对介绍. 2.[hostAdmin]: ...
- UvaLive6662 The Last Ant 模拟
UvaLive6662 PDF题目 题意:给出隧道长度L,蚂蚁数量N,各蚂蚁位置Pi.前进方向Di,都为整数(前进方向为L或R),蚂蚁速度为1cm每秒,两蚂蚁若在整数点相遇则都反向,若不在整数点相遇则 ...
- sql server cpu占用过高优化
SQLSERVER排查CPU占用高的情况 今天中午,有朋友叫我帮他看一下数据库,操作系统是Windows2008R2 ,数据库是SQL2008R2 64位 64G内存,16核CPU 硬件配置还是比较高 ...
- [译]在AngularJS中何时应该使用Directives,Controllers或者Service
原文: http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ Services Servic ...
- EF更新指定字段...
EF更新指定的字段... 搜来搜去发现没有自己想要的啊... 或许本来就有更好的办法来实现我这个,所以没有人来搞吧... 如果有,请不吝告知..GG.. //要更改UserInfo表中指定的列,比如这 ...
- 【转】(笔记)CANopen协议【CANFestival】移植方法
一.背景 CAN组网就必须得要应用层协议,原因就在于 * 便于网络管理与控制 * 确认数据的收发 * 发送大于8个字节的数据块(CAN每帧数据传输大小为8字节) * 为不同节点分配不同的报文标识符 * ...