Flatten 2D Vector -- LeetCode
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的更多相关文章
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- LeetCode Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- LeetCode 251. Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- [LeetCode] 251. Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- 251. Flatten 2D Vector
题目: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,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] ] ...
- Flatten 2D Vector
Implement an iterator to flatten a 2d vector. For example, Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- [Locked] Flatten 2D Vector
Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...
- 251. Flatten 2D Vector 平铺二维矩阵
[抄题]: Implement an iterator to flatten a 2d vector. Example: Input: 2d vector = [ [1,2], [3], [4,5,6 ...
随机推荐
- JMeter学习笔记(六) 文件下载接口测试
本次测试的是文件下载接口,文件是PDF文档,步骤如下: 1.通过jmeter的录制功能,获取了文件下载接口的地址和参数,和其他的HTTP请求一样的配置 2.执行此接口后,察看结果树,点击下载接口的结果 ...
- [译]16-spring基于注解的配置元数据
从spring2.5起spring框架开始支持java注解的配置元数据.所以除了使用xml配置文件来描述bean的装配之外,你还 可以使用基于java注解的配置元数据来完成同样的功能. spring框 ...
- 孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库
孤荷凌寒自学python第五十八天成功使用python来连接上远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第四天.今天的感觉是,mongoDB数据 ...
- 孤荷凌寒自学python第十五天python循环控制语句
孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...
- 孤荷凌寒自学python第三天 初识序列
孤荷凌寒自学python第三天 初识序列 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的序列非常让我着迷,之前学习的其它编程语言中没有非常特别关注过序列这种类型的对象,而pyt ...
- Python——数据类型之list、tuple
本篇主要内容 • list初识 • list元素的访问 • list内部所有的方法 • tuple介绍和与list用法的比较 我觉得Python里面用的最多的就是List了,感觉好强大.他能存 ...
- svn 数据库 账号密码
47.106.107.201 云商传媒李孔文 2018/8/22 19:26:35 root 云商传媒李孔文 2018/8/22 19:26:39 sn123456 云商传媒李孔文 2018/8/22 ...
- easyui中datagrid空数据集不刷新的解决方式
datagrid空间可以异步请求json数据,并将新数据覆盖原有数据,重绘数据表. 但是当回来空数据集的时候,js会产生这样一条错误: TypeError: rows is null for(var ...
- P2135 方块消除
题目描述 Jimmy最近迷上了一款叫做方块消除的游戏.游戏规则如下:n个带颜色方格排成一列,相同颜色的方块连成一个区域(如果两个相邻方块颜色相同,则这两个方块属于同一区域).为简化题目,将连起来的同一 ...
- P4113 [HEOI2012]采花
题目描述 萧薰儿是古国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花. 花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于 ...