Zigzag Iterator 解答
Question
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?
Solution
This solution is suitable for k vectors.
public class ZigzagIterator {
private List<Iterator> list; public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
list = new ArrayList<Iterator>();
if (v1.iterator().hasNext())
list.add(v1.iterator());
if (v2.iterator().hasNext())
list.add(v2.iterator());
} public int next() {
Iterator<Integer> current = list.remove(0);
int result = current.next();
if(current.hasNext())
list.add(current);
return result;
} public boolean hasNext() {
return list.size() > 0 ? true : false;
}
} /**
* 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 ...
- 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 ...
随机推荐
- MVC MVC 路由详解
在项目中我们引用了System.Web.Routing; Routing的作用: 确定Controller 确定Action 确定其他参数 根据识别出来的数据, 将请求传递给Controller和 ...
- Redis安全
安全 执行在可信环境 Redis的安全设计是在"Redis执行在可信环境"这个前提下做出的.在生产环境执行时不能同意外界直接连接到Redisserver上.而应该通过应用程序进行中 ...
- 可失败构造器(Failable Initializers)
Xcode6.1中Swift的最新版本是1.1,在该版本中引入了一个新的特性:可失败构造器.通过构造器初始化实际上是给class或struct的每一个存储属性(参数)提供初始值,进行对象实例化的过程. ...
- 浅谈GitLab与Git
前言:先解释下关于库的认识. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remote:远程仓库 一.新增项目(远程仓库) 在GitLa ...
- ListVeiw新增记录及 滚动条移动到指定位置
C# 自带的ListView控件的滚动条移动到指定位置. lvwList为ListView控件 lvwList.EnsureVisible(lvwList.Items.Count - 1); 新增记录 ...
- DIV+CSS 自适应布局
两栏布局,左边定宽200px,右边自适应.如何实现?我的第一个反应就是:用flex伸缩盒呀,然后balabala...说完之后,面试官说,还有没有别的方法?flex有些浏览器不支持嗯...我愣了一下, ...
- AspNetPager
AspNetPager使用方法引入dll <%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer& ...
- 《第一行代码》学习笔记10-活动Activity(8)
1.除了onRestart()方法,其他都是两两相对的.三种生存期: (1)完整生存期:onCreate()~onDestroy().一般情况下,一个活动会在onCreate()中完成各种初始化操作, ...
- 查看Sql语句执行速度
原文链接:http://www.cnblogs.com/New-world/archive/2012/11/28/2793560.htmlMS_SQL模糊查询like和charindex的对比 lik ...
- css实现超连接按钮形式显示
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...