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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道.next 和 .hasnext有啥区别:取出来、只是看看有没有

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

只有stack才能一次取出来一层,数组不能直接取出来一层。所以用stack。

stack有

.getInteger()
.getList()

方法

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

原文地址:https://www.cnblogs.com/immiao0319/p/9004973.html

时间: 2024-08-14 12:04:31

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

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

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

[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

LeetCode "Flatten Nested List Iterator"

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

codeforces 652D Nested Segments 离散化+树状数组

题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using name