[Locked] Zigzag Iterator
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的更多相关文章
- 281. Zigzag Iterator
题目: Given two 1d vectors, implement an iterator to return their elements alternately. For example, g ...
- 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 ...
- [LeetCode#281] Zigzag Iterator
Problem: Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...
- [Swift]LeetCode281. 之字形迭代器 $ Zigzag Iterator
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- 281. Zigzag Iterator z字型遍历
[抄题]: Given two 1d vectors, implement an iterator to return their elements alternately. Example: Inp ...
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. Example: Input: v1 ...
随机推荐
- DataView操作DataTable
1.DataView筛选数据 //假设有一个DataTable数据 DataTable dt = new DataTable(); //转成DefaultView DataView dv = dt.D ...
- Echarts使用随笔(2)-Echarts中mapType and data
本文出处:http://blog.csdn.net/chenxiaodan_danny/article/details/39081071 series : [ { ...
- 惠普 Compaq 6520s 无线开关打不开
问题描述:键盘上面的无线开关怎么按都打不开,始终是出于黄色的状态 解决方法:尝试恢复bios默认值测试: 开机不停点击F10进入bios,选择File选项,选择Restore Defaults-- ...
- MySQL性能状态查看方式
1. QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...
- java集合之链式操作
如果用过js/jquery.groovy等语言,大概对这样的代码比较熟悉: [1,2,3].map(function(d){...}).grep(function(d){...}).join(',') ...
- 原生Js获取某个节点后面的第一个标签
nextSlbling属性 获取某个节点后面的第一个节点(可能是标签 文本) 判断获取的节点是否为标签节点还是文本节点 window.onload=function(){ var pagecount= ...
- PHP对象类型在内存中的分配
对象类型和整型.字符串等类型一样,也是PHP中的一种数据类型.都是在程序中用于存储不同类型数据使用的,在程序运行时它的每部分内容都要先加载到内存中再被使用.那么对象类型的数据在内存中是如何分配的呢?先 ...
- 百度编辑器ueditor如何配置
完全版: 1.加载 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- linux 和 ubuntu 修改主机名
原始主机名:jeepend-VirtualBox linux 修改方法: 1.使用secure CRT或其他方式 用root用户 登入系统.2.在终端输入: #hostname [修改后的主机名]3. ...
- 编译内核出错:invalid option `abi=aapcs-linux' 解决办法
出现此问题的原因是由于kernel feature中选中了Use the ARM EABIto compile the kernel引起的,有两各解决办法: 1)换编译器为arm-linux-gcc ...