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有啥区别:取出来、只是看看有没有
[一句话思路]:
只有stack才能一次取出来一层,getlist getinteger
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- .next建立在.hasnext的基础之上,所以hasnext需要放在while循环中,做完为止
- curr如果是个list,就必须用专有方法
curr.getList()
先取出,再做后续操作
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
一次放一层
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
list 对应的方法是.size() .get
只有stack才能一次取出来一层,数组不能直接取出来一层。所以用stack。
stack有
.getInteger()
.getList()
方法
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
public NestedIterator(List<NestedInteger> nestedList) {
//put into stack from back
for (int i = nestedList.size() - 1; i >= 0; i--) stack.push(nestedList.get(i));
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public boolean isInteger();
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* public Integer getInteger();
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
//ini:stack
Stack<NestedInteger> stack = new Stack<>(); public NestedIterator(List<NestedInteger> nestedList) {
//put into stack from back
for (int i = nestedList.size() - 1; i >= 0; i--) stack.push(nestedList.get(i));
} @Override
public Integer next() {
//pop
return stack.pop().getInteger();
} @Override
public boolean hasNext() {
while (!stack.isEmpty()) {
//isInteger or put into stack from back
NestedInteger curr = stack.peek();
if (curr.isInteger()) return true; stack.pop();
for (int i = curr.getList().size() - 1; i >= 0; i--) {
stack.push(curr.getList().get(i));
} }
return false;
}
} /**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i = new NestedIterator(nestedList);
* while (i.hasNext()) v[f()] = i.next();
*/
最新文章
- 恢复MySQL主从数据一致性的总结
- 用js解析经json序列化后的C#的DateTime类型数据
- Android 中的缓存机制与实现
- 每天一个linux命令---导出到文件
- linux 多个ssh key 登录远程主机
- sdp内容解析
- MySQL DBA面试全揭秘
- linux系统的文件类型学习
- homework-07
- Nuget找不到服务器
- poj3308
- java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layou
- php7.0 和 php7.1新特性
- pur-ftpd在ubuntu上的安装
- hdu_1251统计难题(字典树Trie)
- JAVA 一句话技巧
- CMDB服务器管理系统【s5day90】:API验证
- F#周报2019年第6期
- Linux下解压.tar.xz格式文件的方法
- OLEDB 命令转换组件的用法