[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 next repeatedly until hasNext returns false,
             the order of elements returned by next should be: [1,1,2,1,1].

Example 2:

Input: [1,[4,[6]]]
Output: [1,4,6]
Explanation: 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, rather than a nested list.
 7  *     public boolean isInteger();
 8  *
 9  *     // @return the single integer that this NestedInteger holds, if it holds a single integer
10  *     // Return null if this NestedInteger holds a nested list
11  *     public Integer getInteger();
12  *
13  *     // @return the nested list that this NestedInteger holds, if it holds a nested list
14  *     // Return null if this NestedInteger holds a single integer
15  *     public List<NestedInteger> getList();
16  * }
17  */
18 public class NestedIterator implements Iterator<Integer> {
19
20   private Stack<NestedInteger> stack;
21
22     public NestedIterator(List<NestedInteger> nestedList) {
23         stack = new Stack<>();
24         pushListElements(nestedList);
25     }
26
27     @Override
28     public Integer next() {
29         return stack.pop().getInteger();
30     }
31
32     @Override
33     public boolean hasNext() {
34         if (stack.isEmpty()) {
35             return false;
36         }
37
38         while (!stack.peek().isInteger()) {
39             pushListElements(stack.pop().getList());
40             if (stack.isEmpty()) {
41                 return false;
42             }
43         }
44         return true;
45     }
46
47     private void pushListElements(List<NestedInteger> list) {
48         for (int i = list.size() - 1; i >= 0; i--) {
49             stack.push(list.get(i));
50         }
51     }
52 }

原文地址:https://www.cnblogs.com/liuliu5151/p/10014700.html

时间: 2024-08-05 05:00:56

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

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

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][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

[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

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

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

python 展开嵌套列表

python 展开嵌套列表 目录 python 展开嵌套列表 引言 方法1,创建递归函数 方法2,使用列表推导式 其他的方法 引言 Python中的列表还可以将不同数据类型的项放在一个列表中.所以,一个嵌套的列表就是是一个包含多个列表的列表,例如[1,2,[3],[4,[5,6]]. 通常,我们需要将这些嵌套列表转换为平面列表(flatted a nested list),以便对数据执行常规列表操作. 方法1,创建递归函数 首先介绍,python中对一个嵌套的list,和一个空列表[],进行su

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