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();
*/

最新文章

  1. MySQL主从同步
  2. 队列-java代码
  3. Error writing file‘frm‘(Errcode: 28)
  4. 回到顶部缓动效果代码 --- tween动画函数库
  5. 【转载】Oracle的方案(Schema)和用户(User)的区别
  6. 【BZOJ】【1269】【AHOI2006】文本编辑器editor
  7. poj 3026 Borg Maze (BFS + Prim)
  8. java新手笔记19 抽象类
  9. 测试网站是共享还是独立ip
  10. 每隔一段时间执行一次函数。window.setTimeout
  11. LINUX系统GIT使用教程
  12. mysql-proxy实现读写分离
  13. 20165323 实验三 敏捷开发与XP实践
  14. PHP封装类 【 设置分页 】 !!! 可以直接引用 !!! 都有自己理解的注释,挺详细的,有搜到的朋友可以能帮到你们 【 新手一看练两遍就懂 】
  15. 大直播时代,P2P才是降低成本的必杀技
  16. java web 大文件下载
  17. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
  18. mac电脑的系统偏好设置的安全与隐私的任何来源没有了
  19. mysql的innodb存储引擎和myisam存储引擎的区别
  20. HDU 2492 树状数组

热门文章

  1. Python面试题(练习三)
  2. 3dMax,Maya与FBX
  3. elk-filebeat收集docker容器日志
  4. UVa 11374 - Airport Express ( dijkstra预处理 )
  5. 关于usr/bin/ld: cannot find -lxxx问题总结(Qt编译错误cannot find -lGL)
  6. 【bzoj1511】[POI2006]OKR-Periods of Words KMP-next数组
  7. [CF522D]Closest Equals
  8. BZOJ 1901: Zju2112 Dynamic Rankings | 带修改主席树
  9. BZOJ5300 [Cqoi2018]九连环 【dp + 高精】
  10. 【CF Round 439 A. The Artful Expedient】