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]
Time Limit exceeded.
/***//This is the interface and allows for creating nested lists. *//You should not implement it, or speculate abou T its implementation * public interface Nestedinteger {* *///@returntrue if this nestedinteger holds a single integer, rather than a nested list. * Public boolean isinteger (); * * // @returnThe single integer Nestedinteger holds, if it holds a single integer *//Return NULL if this nestedint Eger holds a nested list * public Integer getinteger (); * * // @returnthe nested list that this nestedinteger holds, if it holds a nested list *//Return NULL if this nestedinteger h Olds a single integer * Public list<nestedinteger> getList (); * } */ Public classNestediteratorImplementsIterator<integer>{Deque<Integer> indices =NewLinkedlist<integer>(); Deque<List<NestedInteger>> levels =NewLinkedlist<>(); /*** Nestedinteger [1] is treated as a list. * [1].isinteger () = = false; * * Nestedinteger [] is possible. */ PublicNestediterator (list<nestedinteger>nestedlist) { while(Nestedlist.size () > 0) {Indices.push (0); Levels.push (nestedlist); Nestedinteger FirstItem= Nestedlist.get (0); if(Firstitem.isinteger ()) Break; Elsenestedlist=firstitem.getlist (); }} @Override PublicInteger Next () {intCurrentind =Indices.pop (); List<NestedInteger> CurrentLevel =Levels.peek (); Integer ret=Currentlevel.get (currentind). Getinteger (); if(currentind+1<currentlevel.size ()) Indices.push (Currentind+1); Else{levels.pop ();//Current level reaches the end pops out. if(Levels.isempty ())//if levels is empty and then there is no next. returnret; intIndexforlastlevel =Indices.pop (); //Do the loop to reach a level, which is not at the end and not at a empty element. while(true) {CurrentLevel=Levels.peek (); while(indexforlastlevel+1<currentlevel.size ()) {Nestedinteger CurrentItem= Currentlevel.get (indexforlastlevel+1); if(!currentitem.isinteger () &¤titem.getlist (). IsEmpty ())++indexforlastlevel;//Skip the empty list items. } //Reach the end of current level . Go the upper level. if(Indexforlastlevel+1 = =currentlevel.size ()) {Levels.pop (); if(Levels.isempty ())//reached the root. returnret; Indexforlastlevel=Indices.pop (); } Else Break; } indices.push (Indexforlastlevel+1); Nestedinteger CurrentItem= Levels.peek (). Get (indexforlastlevel+1); while(!Currentitem.isinteger ()) {Indices.push (0); List<NestedInteger> Nextlevel =currentitem.getlist (); Levels.push (Nextlevel); CurrentItem= Nextlevel.get (0); } } returnret; } @Override Public BooleanHasnext () {if(Indices.size () > 0) return true; return false; }}/*** Your Nestediterator object would be instantiated and called as such: * nestediterator i = new Nestediterator (Nestedl IST); * while (I.hasnext ()) v[f ()] = I.next (); */
341. Flatten Nested List Iterator