码迷,mamicode.com
首页 > 其他好文 > 详细

Flatten Nested List Iterator

时间:2016-07-22 01:09:03      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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

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

  • 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].

  • 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].

 

 1 /**
 2  * // This is the interface that allows for creating nested lists.
 3  * // You should not implement it, or speculate about its implementation
 4  * public interface NestedInteger {
 5  *
 6  *     // @return true if this NestedInteger holds a single integer,
 7  *     // rather than a nested list.
 8  *     public boolean isInteger();
 9  *
10  *     // @return the single integer that this NestedInteger holds,
11  *     // if it holds a single integer
12  *     // Return null if this NestedInteger holds a nested list
13  *     public Integer getInteger();
14  *
15  *     // @return the nested list that this NestedInteger holds,
16  *     // if it holds a nested list
17  *     // Return null if this NestedInteger holds a single integer
18  *     public List<NestedInteger> getList();
19  * }
20  */
21 import java.util.Iterator;
22 import java.util.Stack;
23 
24 public class NestedIterator implements Iterator<Integer> {
25 
26     Stack<NestedInteger> stack = new Stack<NestedInteger>();
27 
28     public NestedIterator(List<NestedInteger> nestedList) {
29         if (nestedList == null)
30             return;
31 
32         for (int i = nestedList.size() - 1; i >= 0; i--) {
33             stack.push(nestedList.get(i));
34         }
35     }
36 
37     // @return {int} the next element in the iteration
38     @Override
39     public Integer next() {
40         return stack.pop().getInteger();
41     }
42 
43     // @return {boolean} true if the iteration has more element or false
44     @Override
45     public boolean hasNext() {
46         while (!stack.isEmpty()) {
47             NestedInteger top = stack.peek();
48             if (top.isInteger()) {
49                 return true;
50             } else {
51                 stack.pop();
52                 for (int i = top.getList().size() - 1; i >= 0; i--) {
53                     stack.push(top.getList().get(i));
54                 }
55             }
56         }
58         return false;
59     }
60 
61     @Override
62     public void remove() {
63 
64     }
65 }
66 
67 /**
68  * Your NestedIterator object will be instantiated and called as such:
69  * NestedIterator i = new NestedIterator(nestedList); while (i.hasNext())
70  * v.add(i.next());
71  */

 

Flatten Nested List Iterator

Flatten Nested List Iterator

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5693674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!