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].

分析:

  当k不大于2时,这题解法很直观,交替读两个vector,当其中一个读到尾部的时候,设一个读完的标记,直到两个vector都读完;当k大于2时,也可以使用上述方法,不过复杂度将会是O(k*Nmax),其中Nmax为最长一个vector的元素个数,也就是说,当其中一个vector为N,N很大,其他vector都不包含元素时,即为稀疏矩阵时,复杂度也会是O(k*N),即遍历了整个稀疏矩阵,虽然按道理复杂度应该为O(N)才合适,为了解决这个问题,只需要将未读完的数组用一个标记放在某个数据结构里交替来读即可。

  这题我取巧用一个vector先遍历了一遍原稀疏矩阵,事实上并不需要这么做,只需要在类中保存当前i, j以及存储的未访问完vector编号的set即可

代码:

class Solution {
private:
vector<int> result;
int index;
public:
Solution(vector<vector<int> > v) {
index = ;
set<int> myset;
for(int i = ; i < v.size(); i++)
if(!v[i].empty())
myset.insert(i);
int col = ;
while(!myset.empty()) {
auto row = myset.begin();
while(row != myset.end()) {
result.push_back(v[*row][col]);
if(v[*row].size() - == col)
myset.erase(row++);
else
row++;
}
col++;
}
}
int next() {
return result[index++];
}
bool hasNext() {
return index < result.size();
}
};

[Locked] Zigzag Iterator的更多相关文章

  1. 281. Zigzag Iterator

    题目: Given two 1d vectors, implement an iterator to return their elements alternately. For example, g ...

  2. Zigzag Iterator II

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

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

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

  4. Zigzag Iterator

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

  5. LeetCode Zigzag Iterator

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

  6. [LeetCode#281] Zigzag Iterator

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

  7. [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator

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

  8. 281. Zigzag Iterator z字型遍历

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

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

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

随机推荐

  1. 详解SQL Server 2005 Express下的事件探查器

    安装Visual Studio 2008会有附带的SQL Server 2005 Express版 我们开发一般都用那个都不单独安装SQL Server的 大家都知道express版的sql是没有 事 ...

  2. UTF-8和GBK有什么区别?

    字符均使用双字节来表示,只不过为区分中文,将其最高位都定成1. 至于UTF-8编码则是用以解决国际上字符的一种多字节编码,它对英文使用8位(即一个字节),中文使用24位(三个字节)来编码.对于英文字符 ...

  3. PageMapAdapter MapAdapter (续webServices)

    public class PageMapAdapter extends XmlAdapter<PageMapConverter, IPage<Map<String, Object&g ...

  4. requirejs+anjularjs+express框架

    1.目录 2.首页login.html如下: <!DOCTYPE html><html> <head> <title>登录界面</title> ...

  5. Deep Learning 学习随记(五)深度网络--续

    前面记到了深度网络这一章.当时觉得练习应该挺简单的,用不了多少时间,结果训练时间真够长的...途中debug的时候还手贱的clear了一下,又得从头开始运行.不过最终还是调试成功了,sigh~ 前一篇 ...

  6. jdk配置环境变量(windows)

    1.配置环境变量:右击"我的电脑"-->"高级"-->"环境变量"1)在系统变量里新建"JAVA_HOME" ...

  7. prototype/constructor/__proto__之prototype

    1任何对象都有__proto__属性 属性值Object2并不是所有对象都有prototype属性.只有方法对象(构造函数)以及基本数据类型还有Array,有prototype属性;并且所有方法(对象 ...

  8. 9张思维导图学习Javascript

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  9. Java环境配置出现的问题及解决办法

    我使用的安装文件,从博客园一个童鞋那里找到的,连接是http://www.cnblogs.com/SelectError/p/3205582.html#commentform 开发基础环境,版本为Ja ...

  10. STM32学习笔记——定时器中断(向原子哥学习)

    定时器中断 STM32 的定时器功能十分强大,有 TIME1 和 TIME8 等高级定时器,也有 TIME2~TIME5 等通用定时器,还有 TIME6 和TIME7 等基本定时器.在本章中,我们将利 ...