LintCode 528:Flatten Nested List Iterator

来源:互联网 发布:反编译软件为dede 编辑:程序博客网 时间:2024/06/10 05:54
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());    }};

0 0
原创粉丝点击