[LeetCode] Flatten 2D Vector
2015-08-05 15:16
435 查看
Problem Description:
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].
The idea is very simple. We keep two variables
In the constructor, we initialize
In
In
A final and important note, since in
The time complexity of
The code is as follows.
Since we need to copy the vec2d anyway, we can just copy it into a simple vector<int>, which makes life much easier :-)
Of course, since elements in vector are stored in a contiguous range of memory, the problem can be solved in O(1) memory (see here for a better solution).
Implement an iterator to flatten a 2d vector.
For example,
Given 2d vector =
[ [1,2], [3], [4,5,6] ]
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1, 2, 3, 4, 5, 6].
The idea is very simple. We keep two variables
rowand
colfor the range of rows and cols. Specifically,
rowis the number of rows of
vec2dand
colis the number of columns of the current 1d vector in
vec2d. We also keep two variables
rand
cto point to the current element.
In the constructor, we initialize
rowand
colas above and initialize both
rand
cto be
0(pointing to the first element).
In
hasNext(), we just need to check whether
rand
care still in the range limited by
rowand
col.
In
next(), we first record the current element, which is returned later. Then we update the running indexes and possibly the range if the current element is the last element of the current 1d vector.
A final and important note, since in
next(), we record the current element, we need to guarantee that there is an element. So we implement a helper function
skipEmptyVector()to skip the empty vectors. It is also important to handle the case that
vec2dis empty (in this case, we set
col = -1).
The time complexity of
hasNext()is obviously
O(1)and the time complexity of
nextis also
O(1)in an amortized sense.
The code is as follows.
class Vector2D { public: Vector2D(vector<vector<int>>& vec2d) { data = vec2d; r = c = 0; row = vec2d.size(); col = (row == 0 ? -1 : data[r].size()); skipEmptyVector(); } int next() { int elem = data[r][c]; if (c == col - 1) { r++; c = 0; col = data[r].size(); } else c++; skipEmptyVector(); return elem; } bool hasNext() { return col != -1 && (r < row && c < col); } private: vector<vector<int>> data; int row, col, r, c; void skipEmptyVector(void) { while (!col) { r++; col = data[r].size(); } } }; /** * Your Vector2D object will be instantiated and called as such: * Vector2D i(vec2d); * while (i.hasNext()) cout << i.next(); */
Since we need to copy the vec2d anyway, we can just copy it into a simple vector<int>, which makes life much easier :-)
class Vector2D { public: Vector2D(vector<vector<int>>& vec2d) { int row = vec2d.size(); for (int r = 0; r < row; r++) { int col = vec2d[r].size(); for (int c = 0; c < col; c++) data.push_back(vec2d[r][c]); } idx = 0; } int next() { return data[idx++]; } bool hasNext() { return idx < data.size(); } private: vector<int> data; int idx; }; /** * Your Vector2D object will be instantiated and called as such: * Vector2D i(vec2d); * while (i.hasNext()) cout << i.next(); */
Of course, since elements in vector are stored in a contiguous range of memory, the problem can be solved in O(1) memory (see here for a better solution).
相关文章推荐
- Server Tomcat v7.0 Server at localhost failed to start
- 贪心算法
- 数据库集中分页效率的比较
- Undefined symbols for architecture i386:和"_OBJC_CLASS_$_xx", referenced from:问题解决方法
- UVA 146 ID Codes
- 南邮 OJ 1404 取数游戏
- 南邮 OJ 1401 乘车费用
- [转]构建C协程之setjmp/long_jmp篇
- HDU 5351 MZL's Border(规律)
- RTEMS-BBB(四):rki镜像
- c++中的explicit关键字
- 防止Block的循环引用
- UIView的图层关系
- 孙源面试题试解(更新完毕)
- android_移植memtester
- 低级绕过手段
- Pie(poj 3122)二分法
- 64bit需要做的改变
- linux命令--------------top
- 如何在Android开发中让你的代码更有效率