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. 用于显示上个月和下个月_PHP

    /** * 用于显示上个月和下个月 * @param int $sign 1:表示上个月 0:表示下个月 * @return string */ function GetMonth($sign=&qu ...

  2. oracle数据表误删恢复

    1.查看回收站中的表: select object_name,original_name,partition_name,type,ts_name,createtime,droptime from re ...

  3. 关于c:\fakepath\的解决办法

    (2014.11.25 最后更新) 一.碎碎念:关于访问本地图片的路径的问题,比较典型的例子就是上传头像.在以往的解决办法中,我们大多是先将图片上传到服务器然后从服务器返回图片,显示在页面上以达到预览 ...

  4. POJ 2942.Knights of the Round Table (双连通)

    简要题解: 意在判断哪些点在一个图的  奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...

  5. laravel4通过控制视图模板路劲来动态切换主题

    通过控制视图模板路劲来动态切换主题 App::before(function($request) { $paths = Terminal::isMobile() ? array(__dir__.'/v ...

  6. 移动平台3G手机网站前端开发布局技巧汇总

    移动平台3G手机网站前端开发布局技巧汇总 作者:前端开发-武方博   发布:2011-05-10 09:11   分类:移动开发   阅读:120,618 views   7条评论     您或许正在 ...

  7. Windows7 IIS7 无法启动计算机上的服务W3SVC如何修复

    错误提示 启动iis7管理服务器提示:无法启动计算机上的服务W3SVC 启动Windows Process Activation Service服务,报错:6801 指定资源管理器中的事务支持未启动或 ...

  8. 常用排序算法集合-C实现

    之前熟悉C的时候写着玩的,就当做笔记用吧: #include<stdio.h> #include<stdlib.h> #include<string.h> #inc ...

  9. 从一到二:利用mnist训练集生成的caffemodel对mnist测试集与自己手写的数字进行测试

    通过从零到一的教程,我们已经得到了通过mnist训练集生成的caffemodel,主要包含下面四个文件: 接下来就可以利用模型进行测试了.关于测试方法按照上篇教程还是选择bat文件,当然python. ...

  10. while死循环问题-输入字符就会死循环

    问题: 是否会遇到这样的问题,在while循环中 sanf("%d",&a);如果输入的不是数字,是字符就会进入死循环. 解决方案:都是缓冲区惹的祸,输入字符后,字符会一直 ...