2D 벡터를 flat하게 만드는 iterator를 디자인해라.
Input
["Vector2D", "next", "next", "next", "hasNext", "hasNext", "next", "hasNext"]
[[[[1, 2], [3], [4]]], [], [], [], [], [], [], []]
Output
[null, 1, 2, 3, true, true, 4, false]
Explanation
Vector2D vector2D = new Vector2D([[1, 2], [3], [4]]);
vector2D.next(); // return 1
vector2D.next(); // return 2
vector2D.next(); // return 3
vector2D.hasNext(); // return True
vector2D.hasNext(); // return True
vector2D.next(); // return 4
vector2D.hasNext(); // return False
class Vector2D {
public:
vector<int> flat;
int cur = 0;
Vector2D(vector<vector<int>>& vec) {
for (int i = 0; i < vec.size(); i++)
for (int j = 0; j < vec[i].size(); j++)
flat.push_back(vec[i][j]);
}
int next() {
return flat[cur++];
}
bool hasNext() {
return cur < flat.size();
}
};
많은 도움이 되었습니다, 감사합니다.