题目:

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. Hibernate学习---第五节:普通组件和动态组件

    一.普通组件映射配置 1.创建组件类,代码如下: package learn.hibernate.bean; /** * 组件类 */ public class Phones { private St ...

  2. linux内核分析之fork()

    从一个比较有意思的题开始说起,最近要找工作无意间看到一个关于unix/linux中fork()的面试题: #include<sys/types.h> #include<stdio.h ...

  3. C#委托详解(3):委托的实现方式大全(续)

    接上篇(C#委托详解(2):实现方式大全),本篇继续介绍委托的实现方式. 4.Action<T>和Func<T>委托 使用委托时,除了为每个参数和返回类型定义一个新委托类型之外 ...

  4. UVALive - 6575 Odd and Even Zeroes 数位dp+找规律

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48419 Odd and Even Zeroes Time Limit: 3000MS 问题描述 In mat ...

  5. hdu 3007 Buried memory 最远点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3007 Each person had do something foolish along with ...

  6. BZOJ 3511 土地划分

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3511 题目分析: 看上去和前面的人员雇佣以及小M种田都很像. 最小割模型来求最大值,一般都 ...

  7. matrix_last_acm_1

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96950#problem/A 题意:n个数初始ai,m次操作 ...

  8. 笔直的水管 usaco 背包

    背包dp入门,需要滚动数组: #include<iostream> #include<cstdio> #include<string> #include<cs ...

  9. Mybatis 自动从数据库生成entity,mapping,dao接口

    1.下载需要的jar包 mybatis-generator-core-1.3.2.jar,mysql-connector-java-5.1.39.jar 2.把上面的jar包放到某个目录,并在该目录下 ...

  10. phonegap/cordova常用命令

    创建项目 cordova create foldername com.wps.test projectName cd foldername 基本设备信息 设备 API: cordova plugin ...