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 ...
随机推荐
- AlarmManager使用注意事项
在使用AlarmManager实现闹钟需要注意的是,intent和pendingintend的context如果是activity,那么当activity回收之后,context对象则不能被Alarm ...
- 用PHP对数据库内容进行操作(改)
查询页面(用户可见) <body> <table width="80%" border="1" cellpadding="0&quo ...
- UVALive - 7368 Airports DAG图的最小路径覆盖
题目链接: http://acm.hust.edu.cn/vjudge/problem/356788 Airports Time Limit: 3000MS 问题描述 An airline compa ...
- MyEclipse2015 编写js报 'Calculating completion proposals..' has encountered a problem.
前言:编写js(按点后)弹出这个鬼东西,百度不到..估计是破解有问题.只有换版本了. 版本:MyEclipse 2015 stable 1.0 详细错误信息 解决:换成2.0版本
- 【CentOS】Eclipse插件egit使用
1.简介 2.安装 3.配置 4.使用 5.补充说明 参考资料: http://yufenfei.iteye.com/blog/1750124 1.简介 EGit就是一款Eclips ...
- 【UOJ Easy Round #2】
然而UER我也照样跪…… 第一题 忘了取模sad || 操作符将整个区间分成了一些段,每个手机只会执行其中某一段,执行次数为这一段中&&的个数?+1? ans=ans*num[i]+1 ...
- Windows10+IIS7.5上如何配置PHP站点
最近我一直在写PHP,但是我很喜欢微软的开发环境和Windows的硬件环境,我就想在IIS上配置一下PHP站点,这样用起来也比较方便,在经过各位前辈的文章学习后,自己整理了一个比较简单的图片为主的教程 ...
- 【转载】C++编译出现 error C2664: 不能将参数 2 从“const char [5]”转换为“LPCTSTR”解决办法。
编译程序的时候出现这样的错误,原因是在新建MFC项目的时候,设置字符集Unicode的属性. 解决方法一: 在VC2010的解决方案管理器窗口内,右击你的项目“项目”,然后选“属性”(最后一项),再点 ...
- BZOJ: 1084: [SCOI2005]最大子矩阵
NICE 的DP 题,明白了题解真是不错. Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1228 Solved: 622[Submit][Stat ...
- KMP_Best Reward
大意:把一个字符串分成两串,假如一个字符串是回文串就可以加上它的VALUE,否则它的VALUE为0: KMP的特点是可以求出前缀与后面的字符串是否匹配, 注意回文串的特点,所以当我们把回文串反转的时候 ...