Flatten 2D Vector -- LeetCode
2024-09-06 02:54:51
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();
*/
最新文章
- MySQL主从同步
- 队列-java代码
- Error writing file‘frm‘(Errcode: 28)
- 回到顶部缓动效果代码 --- tween动画函数库
- 【转载】Oracle的方案(Schema)和用户(User)的区别
- 【BZOJ】【1269】【AHOI2006】文本编辑器editor
- poj 3026 Borg Maze (BFS + Prim)
- java新手笔记19 抽象类
- 测试网站是共享还是独立ip
- 每隔一段时间执行一次函数。window.setTimeout
- LINUX系统GIT使用教程
- mysql-proxy实现读写分离
- 20165323 实验三 敏捷开发与XP实践
- PHP封装类 【 设置分页 】 !!! 可以直接引用 !!! 都有自己理解的注释,挺详细的,有搜到的朋友可以能帮到你们 【 新手一看练两遍就懂 】
- 大直播时代,P2P才是降低成本的必杀技
- java web 大文件下载
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
- mac电脑的系统偏好设置的安全与隐私的任何来源没有了
- mysql的innodb存储引擎和myisam存储引擎的区别
- HDU 2492 树状数组
热门文章
- Python面试题(练习三)
- 3dMax,Maya与FBX
- elk-filebeat收集docker容器日志
- UVa 11374 - Airport Express ( dijkstra预处理 )
- 关于usr/bin/ld: cannot find -lxxx问题总结(Qt编译错误cannot find -lGL)
- 【bzoj1511】[POI2006]OKR-Periods of Words KMP-next数组
- [CF522D]Closest Equals
- BZOJ 1901: Zju2112 Dynamic Rankings | 带修改主席树
- BZOJ5300 [Cqoi2018]九连环 【dp + 高精】
- 【CF Round 439 A. The Artful Expedient】