[Leetcode] Flatten 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 could 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 is: [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 is: [1,4,6] .

Thinking of solving problems

First, the integer element is recursively grouped into a list, and then traversed by a list of iterators.

Implementation code
//Runtime:6 MS/** *//This was the interface that allows for creating nested lists. *//You should not implement it, or speculate a Bout its implementation * public interface Nestedinteger {* *// @return True if this nestedinteger holds a Single integer, rather than a nested list. * Public boolean isinteger ();  * *// @return The single integer Nestedinteger holds, if it holds a single integer *//return Null if this nestedinteger holds a nested list * public Integer getinteger (); * *// @return The nested list that this 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> {    PrivateList<integer> nums =NewArraylist<> ();PrivateIterator<integer> Iterator =NULL; Public Nestediterator(list<nestedinteger> nestedlist) { for(Nestedinteger nestedinteger:nestedlist)        {Addnestedinteger (Nestedinteger);    } iterator = Nums.iterator (); }@Override     PublicIntegerNext() {returnIterator.next (); }@Override     Public Boolean Hasnext() {returnIterator.hasnext (); }Private void Addnestedinteger(Nestedinteger Nestedinteger) {if(Nestedinteger.isinteger ())        {Nums.add (Nestedinteger.getinteger ()); }Else{list<nestedinteger> nestedlist = nestedinteger.getlist (); for(Nestedinteger nested:nestedlist)            {Addnestedinteger (nested); }        }    }}/** * Your Nestediterator object would be instantiated and called as such: * nestediterator i = new Nestediterator (nest Edlist); * while (I.hasnext ()) v[f ()] = I.next (); */

[Leetcode] Flatten 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.