LeetCode 341. Flatten Nested List Iterator(嵌套列表迭代器)
来源:互联网 发布:如何检查网络是否丢包 编辑:程序博客网 时间:2024/06/05 19:20
原题网址:https://leetcode.com/problems/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]
.
方法:使用栈来保持当前状态和嵌套关系。
/** * // 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> { private Stack<Cursor> stack = new Stack<>(); public NestedIterator(List<NestedInteger> nestedList) { if (nestedList != null) stack.push(new Cursor(nestedList)); } @Override public Integer next() { while (!stack.isEmpty()) { Cursor cursor = stack.peek(); if (cursor.i < cursor.list.size()) { NestedInteger nested = cursor.list.get(cursor.i++); if (nested.isInteger()) return nested.getInteger(); stack.push(new Cursor(nested.getList())); } else { stack.pop(); } } return null; } @Override public boolean hasNext() { while (!stack.isEmpty()) { Cursor cursor = stack.peek(); if (cursor.i < cursor.list.size()) { NestedInteger nested = cursor.list.get(cursor.i); if (nested.isInteger()) return true; cursor.i ++; stack.push(new Cursor(nested.getList())); } else { stack.pop(); } } return false; }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */class Cursor { List<NestedInteger> list; int i; Cursor(List<NestedInteger> list) { this.list = list; }}
另一种实现方式:
/** * // 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> { private Stack<State> stack = new Stack<>(); public NestedIterator(List<NestedInteger> nestedList) { stack.push(new State(nestedList)); } @Override public Integer next() { hasNext(); State state = stack.peek(); return state.nestedList.get(state.pos++).getInteger(); } @Override public boolean hasNext() { while (!stack.isEmpty()) { State state = stack.peek(); if (state.pos < state.nestedList.size()) { if (state.nestedList.get(state.pos).isInteger()) return true; stack.push(new State(state.nestedList.get(state.pos++).getList())); } else { stack.pop(); } } return false; }}class State { List<NestedInteger> nestedList; int pos; State(List<NestedInteger> nestedList) { this.nestedList = nestedList; }}/** * Your NestedIterator object will be instantiated and called as such: * NestedIterator i = new NestedIterator(nestedList); * while (i.hasNext()) v[f()] = i.next(); */
0 0
- LeetCode 341. Flatten Nested List Iterator(嵌套列表迭代器)
- leetcode 341. Flatten Nested List Iterator
- [leetcode] 341. Flatten Nested List Iterator
- LeetCode 341. Flatten Nested List Iterator
- LeetCode 341. Flatten Nested List Iterator
- [leetcode] 341. Flatten Nested List Iterator
- LeetCode: 341. Flatten Nested List Iterator
- Leetcode 341. Flatten Nested List Iterator
- 【LeetCode】 341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- LeetCode 341. Flatten Nested List Iterator
- [leetcode]341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- leetcode 341. Flatten Nested List Iterator
- Leetcode 341. Flatten Nested List Iterator
- *[Lintcode]Flatten Nested List Iterator 摊平嵌套的列表
- LeetCode *** 341. Flatten Nested List Iterator(审题审题!!)
- 告诉你TestNG annotations注释都是什么时候运行的
- hdu2795 Billboard(单点更新)
- c/c++常用宏、预编译指令
- 等价类
- JQuery EasyUI datagrid 复杂表头处理
- LeetCode 341. Flatten Nested List Iterator(嵌套列表迭代器)
- Linux内核分析总结
- coursera 机器学习课程笔记(一)——回归问题
- 设计模式(十)观察者模式
- iOS视图控制器编程指南 --- 视图控制器所扮演的角色
- Easui中datagrid实现动态控制columns
- Django项目部署 - 脚本自动化
- Jquery之访问元素内容
- MF图C中作流程