[Leetcode] Flatten Nested list Iterator flattened nested list iterator

Source: Internet
Author: User

Given a nested list of integers, implement an iterator to flatten it.

Each element was either an integer, or a list--whose elements may also be integers or other lists.

Example 1:
Given the list [[1,1],2,[1,1]] ,

By calling next repeatedly until hasnext returns FALSE, the order of elements returned by next should be: [1,1,2,1,1] .

Example 2:
Given the list [1,[4,[6]]] ,

By calling next repeatedly until hasnext returns FALSE, the order of elements returned by NEXT should be: [1,4,6] .

This problem let us establish a flattening nested list of iterators, about the data structure of the nested linked list first appeared in the nested list Weight sum, and that the problem is the recursive method to solve, and the iterator is generally used to solve the iterative method, and recursive generally need to use the stack to assist the traversal, Because of the last-in-first-out nature of the stack, when we traverse the vector, we push the object into the stack from the back, then the first object is pressed into the stack and the first one is taken out, and our hasnext () function needs to traverse the stack and handle it, if the top element of the stack is an integer, returns True, Then remove the top element of the stack and begin to traverse the list that is taken out, or push it back forward into the stack, the loop stop condition is the stack is empty, return false, see the code as follows:

Solution One:

classNestediterator { Public: nestediterator (Vector<NestedInteger> &nestedlist) {         for(inti = nestedlist.size ()-1; I >=0; --i) {s.push (nestedlist[i]); }    }    intNext () {Nestedinteger T=s.top (); S.pop (); returnT.getinteger (); }    BOOLHasnext () { while(!S.empty ()) {Nestedinteger T=S.top (); if(T.isinteger ())return true;            S.pop ();  for(inti = T.getlist (). Size ()-1; I >=0; --i) {S.push (t.getlist () [i]); }        }        return false; }
Private: Stack<NestedInteger>s;};

Although iterators are iterative, we can force the use of recursive solutions, how to force the method, that is, we use a queue of queues, at the time of the constructor using the iterative method to flatten the nested list, and then call Hasnext () and Next () is very simple:

Solution Two:

classNestediterator { Public: nestediterator (Vector<NestedInteger> &nestedlist)    {Make_queue (nestedlist); }    intNext () {intt =Q.front (); Q.pop (); returnT; }    BOOLHasnext () {return!Q.empty (); }    Private: Queue<int>Q; voidMake_queue (Vector<nestedinteger> &nestedlist) {         for(Auto a:nestedlist) {if(A.isinteger ()) Q.push (A.getinteger ()); ElseMake_queue (A.getlist ()); }    }};

Similar topics:

Nested List Weight Sum

Flatten 2D Vector

Zigzag Iterator

Resources:

Https://leetcode.com/discuss/95841/simple-solution-with-queue

Https://leetcode.com/discuss/95892/concise-c-without-storing-all-values-at-initialization

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Flatten Nested list Iterator flattened nested list iterator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.