Flatten Nested List Iterator

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

时间: 2024-12-08 00:15:39

Flatten Nested List Iterator的相关文章

[LeetCode][Python]Flatten Nested List Iterator

Flatten Nested List Iterator 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. Example 1:Given the list [[1,1],2,[1,1]], By callin

[LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

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. ExampleGiven the list [[1,1],2,[1,1]], By calling next repeatedly until hasNext

341. Flatten Nested List Iterator

https://leetcode.com/problems/flatten-nested-list-iterator/#/description 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. Example

leetcode 341. Flatten Nested List Iterator

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. Example 1: Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasN

[LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

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. Example 1: Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasN

341. Flatten Nested List Iterator展开多层数组

[抄题]: 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. Example 1:Given the list [[1,1],2,[1,1]], By calling next repeatedly until

[leetcode]341. Flatten Nested List Iterator 展开嵌套列表的迭代器

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. Example 1: Input: [[1,1],2,[1,1]] Output: [1,1,2,1,1] Explanation: By calling ne

LeetCode &quot;Flatten Nested List Iterator&quot;

Besides the common stack-based solutions, here is another recursive NestedIterator solution :) class NestedIterator { int currInx; const vector<NestedInteger> &rData; NestedIterator *sub; void move() { if (currInx < 0 || rData[currInx].isInte

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.