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. mysql之select查询:练习

    单表查询: 数据查询命令:select 识别要查询的列 from识别要查询的表 select 运算符: + .-.*./. 加减乘除 等于= 不等于!= 或 <> 大于等于>= 小于 ...

  2. Mecanim动画

    1.基础 现在Animation编辑器给个模型设计一个动画,都会自动为此模型加上Animator组件,并产生一个controller后缀的控制器和一个相关的anim后缀的动画剪辑, unity根据An ...

  3. Linux开启MySQL远程连接

    Linux开启MySQL远程连接的设置步骤 . MySQL默认root用户只能本地访问,不能远程连接管理MySQL数据库,那么Linux下如何开启MySQL远程连接?设置步骤如下: 1.GRANT命令 ...

  4. jQuery选择器示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. JS计算器(自制)

    <!doctype html><html><header><meta charset="utf-8"><script src= ...

  6. 转换 nvarchar 值 '2013071200000578' 时溢出了整数列

    sqlserver 把一个nvarchar 与 int  类型 拼接 会自动转换 INT 做运算,nvarchar 类型有16位 转换失败 只能 str(int) 转换成 字符型 进行拼接 (sqls ...

  7. Codeforces755D PolandBall and Polygan

    题目戳这里 我们只需要计算每增加一条线后穿过了几条已有的线即可.为了方便,我们令\(K \le N/2\),并且给每条线一个方向,即\(x\)到\((x+K) \; mod \; N\).然后我们假设 ...

  8. 1031. 高一学堂 (at)

    题目描述 在美丽的中山纪念中学里面,有一座高一学堂.所谓山不在高,有仙则名:水不在深,有龙则灵.高一学堂,因为有了yxr,就成了现在这个样子 = =. 由于yxr的语言太过雷人,每次他发微往往都会有一 ...

  9. zepto方法

    1.$.inArray $.inArray v1.0+ $.inArray(element, array, [fromIndex]) ⇒ number 搜索数组中指定值并返回它的索引(如果没有找到则返 ...

  10. package-lock.json 文件的作用

    npm5之后安装文件之后会多出一个package-lock.json的文件,它的作用是: 1. 安装之后锁定包的版本,手动更改package.json文件安装将不会更新包,想要更新只能使用 npm i ...