Flatten Nested List Iterator

Source: Internet
Author: User

Topic links

This topic means to implement an iterator. There is a way to do this online.

 Public  class nestediterator implements Iterator<Integer> {      int Index=0; List<integer> Vals; PublicNestediterator (list<nestedinteger> nestedlist) {vals = parser (nestedlist); } Publiclist<integer> parser (list<nestedinteger> nestedlist) {list<integer> res =NewArraylist<integer> (); for(Nestedinteger n:nestedlist) {if(N.isinteger ())              {Res.add (N.getinteger ()); }Else{Res.addall (Parser (N.getlist ())); }          }returnRes } PublicInteger Next () {if(Index< Vals.size ()) {intval = Vals.get (Index);Index++;returnVal }Else{return-1; }      } Public BooleanHasnext () {return! (Index>= vals.size ()); }  }

This method is simple and rough. However, this is not an ideal implementation, because this implementation causes memory usage to increase by one-fold. My own way of achieving this is

Import Java.util.deque;import java.util.iterator;import java.util.linkedlist;import java.util.List;InterfaceNestedinteger {//@return True if this nestedinteger holds a single integer, rather than a nested list.       PublicBooleanIsinteger();//@return The single integer which this nestedinteger holds, if it holds a single integer      //Return NULL if this nestedinteger holds a nested list       PublicIntegerGetinteger();//@return The nested list that this nestedinteger holds, if it holds a nested list      //Return NULL if this nestedinteger holds a single integer       PublicList<nestedinteger>getList(); } Public classNestediterator implements Iterator<integer> {StaticDeque<list<nestedinteger>> stack=NewLinkedlist<list<nestedinteger>> ();StaticDeque<integer> stackindex=NewLinkedlist<integer> (); List<nestedinteger> nestedlist; Public Nestediterator(list<nestedinteger> nestedlist)        {Stack.add (nestedlist); Stackindex.add (0); } @Override PublicIntegerNext() {intCurrindex=stackindex.polllast ();        List<nestedinteger> Currlist=stack.peeklast (); Nestedinteger currele=currlist.Get(Currindex);if(!currele.isinteger ())            {Stack.addlast (Currele.getlist ()); Stackindex.addlast (0);returnNext (); }intResult=currele.getinteger ();if(currindex+1>=currlist.size ()) {stack.polllast (); }Else{Stackindex.addlast (currindex+1); }returnResult } @Override PublicBooleanHasnext() {if(stack.size () = =0)        {return false; }Else{intCurrindex=stackindex.peeklast (); List<nestedinteger> Currlist=stack.peeklast ();if(!currlist.Get(Currindex). Isinteger () &&currlist.Get(Currindex). GetList () = =NULL) {stack.polllast ();if(currindex+1>=currlist.size ()) {stackindex.polllast (); }Else{stackindex.polllast (); Stackindex.addlast (currindex+1); }returnHasnext (); }        }return true; }}/** * Your Nestediterator object would be instantiated and called as such: * nestediterator i = new Nestediterator (nest Edlist); * while (I.hasnext ()) v[f ()] = I.next (); */

But. This algorithm can not handle [1,2,3,[]] This situation, it may be I did not understand [] the expression in the OJ. But as long as there is no such situation, the code is working properly

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.