class NestedIterator {
vector<int> flattenList;
vector<int>::iterator p;
void flatten(vector<NestedInteger> nestedList){
int i;
for(i=0;i<nestedList.size();i++){
if(nestedList[i].isInteger())
flattenList.push_back(nestedList[i].getInteger());
else
flatten(nestedList[i].getList());
}
return;
}
public:
NestedIterator(vector<NestedInteger> &nestedList) {
// Initialize your data structure here.
flatten(nestedList);
p=flattenList.begin();
}
// @return {int} the next element in the iteration
int next() {
// Write your code here
int result=*p;
p++;
return result;
}
// @return {boolean} true if the iteration has more element or false
bool hasNext() {
// Write your code here
return (p!=flattenList.end());
}
};
LintCode 528:Flatten Nested List Iterator
最新推荐文章于 2021-05-01 15:45:26 发布