LeetCode341. Flatten Nested List iterator

Source: Internet
Author: User
Solution1

We could use a stack to perform the iteration.

In the constructor we push the elements of Nestedlistinto's stack from back to front. So while we call Pop (), the very-a-nestedlist is returned. Afterwards, in the Hasnext function, we check the "top" element in the stack. If It is an Integer, return true. Otherwise, we take out every element of the "list and push them to" stack from back to front (flatten again). Until we find an Integer. The reason to does so are there may a lot of nested empty lists. Thus until we actually find an integer and we are not sure if there is a next integer.

Suppose the number of element we have to check, in the Nestedlist, is N. Then The time complexity is O (n).

/** *//This is the interface so allows for creating nested lists. *//should not implement it, or speculate to its implementation * public interface Nestedinteger {* * *//@
 Return true if this is Nestedinteger holds a single integer, rather than a nested list.
 * Public boolean isinteger (); * *//@return The single integer so this is Nestedinteger holds, if it holds a single integer *//return NULL I
 F This nestedinteger holds a nested list * public Integer getinteger ();  * *//@return The nested list that is Nestedinteger holds, if it holds a nested list *//return NULL if this
 Nestedinteger holds a single-integer * public list<nestedinteger> getlist ();

    */public class Nestediterator implements iterator<integer> {deque<nestedinteger> stack;
        Public Nestediterator (list<nestedinteger> nestedlist) {this.stack = new arraydeque<> (); for (int i = Nestedlist.size ()-1; I &Gt;= 0;
        i--) {Stack.push (Nestedlist.get (i));
        @Override public Integer Next () {if (!this.hasnext ()) {return null;
    Return Stack.pop (). Getinteger (); @Override public boolean Hasnext () {while (!stack.isempty ()) {Nestedinteger Curr = stack.
            Pop ();
                if (Curr.isinteger ()) {Stack.push (Curr);
            return true; else {for (int i = Curr.getlist (). Size ()-1; I >= 0; i--) {Stack.push (curr.get
                List (). get (i));
    }} return false; }/** * Your Nestediterator object would be instantiated and called as such: * nestediterator i = new Nestediterator (n
 Estedlist);
 * while (I.hasnext ()) v[f ()] = I.next (); */
Solution2

Rather than perform the flatten process on the fly, we could flatten the nextedlist the completely .

Use a flattenhelper () function to flatten the nextedlist recursively into a flattenedlist. And thus we could construct a iterator<integer> based on the flattenedlist we got.

This is actually quicker than the previous one although they the share time and space asymptotic. This could due "a real iterator<integer> are more efficient."

public class Nestediterator implements iterator<integer> {private list<integer>
    Flattenedlist;
    Private iterator<integer> it; 
        Public Nestediterator (list<nestedinteger> nestedlist) {this.flattenedlist = new arraylist<integer> ();
        Flattenhelper (nestedlist);
    this.it = This.flattenedList.iterator ();
            } private void Flattenhelper (List<nestedinteger> nestedlist) {for (Nestedinteger i:nestedlist) {
            if (I.isinteger ()) {This.flattenedList.add (I.getinteger ());
            else {flattenhelper (i.getlist ());
    @Override public Integer Next () {return this.it.next ();
    @Override public boolean Hasnext () {return this.it.hasNext (); }
}

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.