原题目:https://leetcode-cn.com/problems/flatten-nested-list-iterator/
思路:深度搜索
代码:
class NestedIterator {
vector<int> ans;
int index = 0;
void dfs(vector<NestedInteger> &nestedList){
for(int i=0;i<nestedList.size();i++){
if(nestedList[i].isInteger()){
ans.push_back(nestedList[i].getInteger());
}
else{
dfs(nestedList[i].getList());
}
}
}
public:
NestedIterator(vector<NestedInteger> &nestedList) {
dfs(nestedList);
}
int next() {
return ans[index++];
}
bool hasNext() {
return index<ans.size();
}
};