281. 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]
.
链接: http://leetcode.com/problems/zigzag-iterator/
题解:
Zigzag Iterator。最简单的就是用两个index分别遍历两个list,然后用一个boolean变量来控制何时遍历哪一个list。 好像我这么做是有问题的,应该用Interator<>来做。
Time Complexity - O(n), Space Complexity - O(1)
public class ZigzagIterator {
private int length;
private int index1 = 0;
private int index2 = 0;
private List<Integer> list1;
private List<Integer> list2;
private boolean useList2; public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
this.length = v1.size() + v2.size();
list1 = v1;
list2 = v2;
if(list1.size() == 0) {
useList2 = true;
}
} public int next() {
if(!useList2) {
if(index2 < list2.size()) {
useList2 = true;
}
return list1.get(index1++);
} else {
if(index1 < list1.size()) {
useList2 = false;
}
return list2.get(index2++);
} } public boolean hasNext() {
return (index1 + index2) < length;
}
} /**
* Your ZigzagIterator object will be instantiated and called as such:
* ZigzagIterator i = new ZigzagIterator(v1, v2);
* while (i.hasNext()) v[f()] = i.next();
*/
Reference:
https://leetcode.com/discuss/57961/o-n-time-%26-o-1-space-java-solution
https://leetcode.com/discuss/63037/simple-java-solution-for-k-vector
https://leetcode.com/discuss/58012/short-java-o-1-space
https://leetcode.com/discuss/71857/clean-java-solution-works-for-k-lists
281. Zigzag Iterator的更多相关文章
- [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 ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- 【LeetCode】281. Zigzag Iterator 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...
- [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 ...
随机推荐
- 表格实现hao123
一.表格实现hao123用到的标签元素 1.[width][bordercolor][cellpadding][rules="none"隐藏表格内线框][border] 例如: & ...
- "Mac OS X"想要进行更改。键入管理员的名称和密码以允许执行此操作("Mac OS X"想使用系统钥匙串)
不知什么时候开始,每次我在运行xcode在真机上,或者archive打包的时间,都会弹出输入用户名和密码的框,搞的烦死了: 解决方法: 打开钥匙串访问 双击那些密钥弹出框: 改变到允许所有应用程序访问 ...
- 十个优秀的C语言学习资源推荐
学习C语言,需要一点一滴,沉下心来,找个安静的地方,泡上一杯咖啡,在浓郁的香味中一起品味她.-- Boatman Yang 人们通常认为计算机编程很烦,但是有些人却从中发现了乐趣.每一个程序员不得不跟 ...
- django构建blog--页面部分(eclipse+pydev)
本文介绍的是在eclipse+pydev 平台下,利用django 搭建blog的第2部分:页面部分(主要涉及3个部分:模板.视图.URL模式) 篇幅1:创建模板 blog目录下新建一个文件夹:tem ...
- netty 入门
先啰嗦两句,使用 netty 来搭建服务器程序,可以发现相比于传统的 nio 程序, netty 的代码更加简洁,开发难度更低,扩展性也很好,非常适合作为基础通信框架. 下面上代码: Server p ...
- 【转载】Mybatis多参数查询映射
转载地址:http://www.07net01.com/zhishi/402787.html 最近在做一个Mybatis的项目,由于是接触不久,虽然看了一下资料,但在实际开发中还是暴 露了很多问题,其 ...
- 使用CSS禁止textarea调整大小功能的方法
这篇文章主要介绍了使用CSS禁止textarea调整大小功能的方法,禁止可以调整textarea大小功能的方法很简单,使用CSS的resize属性即可,需要的朋友可以参考下 如果你使用谷歌浏览器或火狐 ...
- IDA*
模拟退火 基本思路(Main Thoughts): IDA*是一种优秀的搜索法,在一般的实际问题中,它比普通的搜索更快. 通过迭代加深和估价函数剪枝来搜索. 通常处理没有层数上界或上界很多大的搜索. ...
- 【转载】CreateProcess的用法
第一.第二个参数的用法: 例子: 使用ie打开指定的网页. 注意第二个参数是 可执行文件+命令行参数 #include "stdafx.h" #include <window ...
- 关于High-Resolution Timer(了解)
如果一个系统包含高精度性能计数器(HRPC,high-resolution performance counter)则此系统提供高精度定时器.你可以使用API函数QueryPerformanceFre ...