fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. interface NestedInteger {
  11. public Integer getInteger();
  12. public boolean isInteger();
  13. public List<NestedInteger> getList();
  14. }
  15.  
  16. static class NestedIntegerIterator {
  17. private final List<NestedInteger> list;
  18. private Integer nextInteger;
  19. private Stack<Iterator<NestedInteger>> stack;
  20.  
  21. public NestedIntegerIterator(List<NestedInteger> list) {
  22. this.list = list;
  23. stack = new Stack<Iterator<NestedInteger>>();
  24. stack.push(list.iterator());
  25. advance();
  26. }
  27.  
  28. public boolean hasNext() {
  29. return stack.empty() == false;
  30. }
  31.  
  32. public Integer next() {
  33. Integer next = this.nextInteger;
  34. advance();
  35. return next;
  36. }
  37.  
  38. private void advance() {
  39. while (stack.empty() == false) {
  40. Iterator<NestedInteger> it = stack.peek();
  41. if (it.hasNext() == false) {
  42. stack.pop();
  43. } else {
  44. NestedInteger ni = it.next();
  45. if (ni.isInteger()) {
  46. this.nextInteger = ni.getInteger();
  47. break;
  48. } else {
  49. stack.push(ni.getList().iterator());
  50. }
  51. }
  52. }
  53. }
  54. }
  55.  
  56. static class NestedIntegerImpl implements NestedInteger {
  57. private final Integer val;
  58.  
  59. public NestedIntegerImpl(int val) {
  60. this.val = val;
  61. }
  62.  
  63. public Integer getInteger() {
  64. return this.val;
  65. }
  66.  
  67. public boolean isInteger() {
  68. return true;
  69. }
  70.  
  71. public List<NestedInteger> getList() {
  72. return null;
  73. }
  74. }
  75.  
  76. static class NestedIntegerListImpl implements NestedInteger {
  77. private List<NestedInteger> list;
  78.  
  79. public NestedIntegerListImpl(List<NestedInteger> list) {
  80. this.list = new ArrayList<NestedInteger>();
  81. for (NestedInteger ni : list) {
  82. this.list.add(ni);
  83. }
  84. }
  85.  
  86. public Integer getInteger() {
  87. return null;
  88. }
  89.  
  90. public boolean isInteger() {
  91. return false;
  92. }
  93.  
  94. public List<NestedInteger> getList() {
  95. return list;
  96. }
  97. }
  98.  
  99. public static void main (String[] args) throws java.lang.Exception
  100. {
  101. // {0,{1,2}, 3 ,{4,{5, 6}}}
  102. List<NestedInteger> l1 = new ArrayList<NestedInteger>();
  103. List<NestedInteger> l2 = new ArrayList<NestedInteger>();
  104. List<NestedInteger> l3 = new ArrayList<NestedInteger>();
  105. List<NestedInteger> l4 = new ArrayList<NestedInteger>();
  106.  
  107. l4.add(new NestedIntegerImpl(5));
  108. l4.add(new NestedIntegerImpl(6));
  109.  
  110. l3.add(new NestedIntegerImpl(4));
  111. l3.add(new NestedIntegerListImpl(l4));
  112.  
  113. l2.add(new NestedIntegerImpl(1));
  114. l2.add(new NestedIntegerImpl(2));
  115.  
  116. l1.add(new NestedIntegerImpl(0));
  117. l1.add(new NestedIntegerListImpl(l2));
  118. l1.add(new NestedIntegerImpl(3));
  119. l1.add(new NestedIntegerListImpl(l3));
  120.  
  121. NestedIntegerIterator it1 = new NestedIntegerIterator(l1);
  122. while(it1.hasNext()) {
  123. System.out.println(it1.next());
  124. }
  125.  
  126. NestedIntegerIterator it2 = new NestedIntegerIterator(l2);
  127. while(it2.hasNext()) {
  128. System.out.println(it2.next());
  129. }
  130. }
  131. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
6
1
2