题目:

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的更多相关文章

  1. [LeetCode] 281. Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...

  2. [LeetCode#281] Zigzag Iterator

    Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

  3. 281. Zigzag Iterator z字型遍历

    [抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...

  4. 【LeetCode】281. Zigzag Iterator 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 deque 日期 题目地址:https://leetc ...

  5. [Locked] Zigzag Iterator

    Zigzag Iterator Given two 1d vectors, implement an iterator to return their elements alternately. Fo ...

  6. Zigzag Iterator II

    Description Follow up Zigzag Iterator: What if you are given k 1d vectors? How well can your code be ...

  7. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  8. Zigzag Iterator

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  9. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...

随机推荐

  1. c++实例化对象

    今天看到c++实例化对象,有点懵了.Activity_Log the_log (theLogPtr, Tree->GetBranch());这是那一段小代码,开始没看懂.java看习惯了总喜欢n ...

  2. 格式化输出[parts/iomanip]

    /* 用ios类中的成员函数来进行IO格式的控制总需要写一条单独的语句,而不能直接嵌入到IO语句中,显得很不方便,因此C++又提供了一种用操作符来控制IO的格式.操作符分为带参和不带参两种,带参的定义 ...

  3. xml基础学习笔记03

    继续上篇xml学习笔记,坚持.坚持.再坚持啊.... 本篇主要记录: 35.XML节点的删除与修改 36集.用XML制作RSS订阅源 <?php /* 笔记: 35.XML节点的删除与修改 使用 ...

  4. 查看python selenium的api

    打开命令行工具,doc中输入: python -m pydoc -p 然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules 按ctrl+f ...

  5. Reveal 破解

    永久试用Reveal,只需要打开 ~/Library/Preferences/com.ittybittyapps.Reveal.plist 把IBAApplicationPersistenceData ...

  6. vmware workstation 10.0

    2013.9.3 vmware workstation 10.0 build 1295980新增功能– 可以将windows 8.1物理pc转变为虚拟机:unity模式增强,与windows 8.1 ...

  7. bzoj 2327 构图暴力判断+独立集个数

    首先我们可以处理出10^6以内的所有的勾股数,如果我们有2*i-1和2*j互质, 那么A=(2*i-1)*(2*i-1)+(2*i-1)*(2*j),B=2*j*j+(2*i-1)*(2*j)为互质 ...

  8. C++ Template之函数模版

    函数模版的定义: template <typename T> T const& max(const T& a,const T b) { return a > b ? ...

  9. 【转载】让c++ 函数返回一个数组

    在c++中是不允许数组作为函数的返回值的 int [] someFunction( ); //ILLEGAL 要想实现函数返回一个数组,那返回对应数组里面类型的指针 you must return a ...

  10. NGP处理包

    NGP处理部分(主要就是这个RunOnce函数,客户单肯定是开个线程取调用这个RunOnce的) void NGP::RunOnce() { m_spTimerFac->driveTimer() ...