Implement an iterator to flatten a 2d vector.

For example,
Given 2d vector =

[
[,],
[],
[,,]
]

By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6].

 class Vector2D {
public:
int height, nextRow, nextCol;
vector<vector<int> > vec;
Vector2D(vector<vector<int>>& vec2d) {
vec = vec2d;
height = vec.size();
nextRow = nextCol = ;
} int next() {
int res = vec[nextRow][nextCol++];
if (nextCol == vec[nextRow].size()) {
nextCol = ;
nextRow++;
}
return res;
} //skip empty sub arrays
bool hasNext() {
while (nextRow < vec.size() && nextCol == vec[nextRow].size()) {
nextCol = ;
nextRow++;
}
return nextRow < vec.size();
}
}; /**
* Your Vector2D object will be instantiated and called as such:
* Vector2D i(vec2d);
* while (i.hasNext()) cout << i.next();
*/

Flatten 2D Vector -- LeetCode的更多相关文章

  1. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  2. LeetCode Flatten 2D Vector

    原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...

  3. LeetCode 251. Flatten 2D Vector

    原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...

  4. [LeetCode] 251. Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  5. 251. Flatten 2D Vector

    题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6 ...

  6. [Swift]LeetCode251.展平二维向量 $ Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  7. Flatten 2D Vector

    Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  8. [Locked] Flatten 2D Vector

    Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...

  9. 251. Flatten 2D Vector 平铺二维矩阵

    [抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...

随机推荐

  1. JavaScript显示当前时间的操作

    JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标 ...

  2. python3知识点之---------字符串的介绍

    1. 定义 其实字符串就是一系列字符,用引号括起来的就是字符串,其中的引号可以是单引号或者双引号. 比如 "This is a string"   'This is a strin ...

  3. Python MySQLdb 模块使用方法

    import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="root",pass ...

  4. HDU 3954 Level up (线段树特殊懒惰标记)

    http://blog.csdn.net/acm_cxlove/article/details/7548087 感觉最巧的是定义了min_dis……将区间内有无英雄升级分开处理 #include &l ...

  5. Linux中awk后面的RS, ORS, FS, OFS 含义

    转载自http://blog.csdn.net/qq416647781/article/details/40649419   一.RS 与 ORS 差在哪   我们经常会说,awk是基于行列操作文本的 ...

  6. sharePreference的几个重点

    一.  SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中,文件存放在/data/data/&l ...

  7. vs 2012 未能找到与约束contractName Microsoft.VisualStudio.Utilities...匹配的导出

    系统自动更新后,打开项目进行维护时,居然出错了,报的错误信息为“未能找到与约束contractName Microsoft.VisualStudio.Utilities...匹配的导出” 上网查了下, ...

  8. iOS大神班笔记04-View的加载

    iOS开发中一个控制器创建View的过程(注意标注的地方): 1.通过storyboard加载 UIStoryboard的三个方法: + (UIStoryboard *)storyboardWithN ...

  9. P4285 [SHOI2008]汉诺塔

    题目描述 汉诺塔由三根柱子(分别用A.B.C表示)和n个大小互不相同的空心盘子组成.一开始n个盘子都摞在柱子A上,大的在下面,小的在上面,形成了一个塔状的锥形体. 对汉诺塔的一次合法的操作是指:从一根 ...

  10. [洛谷P1972][SDOI2009]HH的项链

    题目大意:给你一串数字,多次询问区间内数字的种类数 题解:莫队 卡点:洛谷数据加强,开了个$O(2)$ C++ Code: #include <cstdio> #include <a ...