Leetcode: Flatten Nested List Iterator
2023-09-11 14:14:07 时间
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].
非常精巧地使用stack。push all the nestedList into the stack from back to front,so when we pop the stack, it returns the very first element
执行hasNext()的时候,如果peek()是integer, return true;否则是一个List<NestedInteger>, 则flatten这个list,同样的,把list里的NestedInteger倒序push入栈
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 Stack<NestedInteger> st; 20 21 public NestedIterator(List<NestedInteger> nestedList) { 22 st = new Stack<NestedInteger>(); 23 for (int i=nestedList.size()-1; i>=0; i--) { 24 st.push(nestedList.get(i)); 25 } 26 } 27 28 @Override 29 public Integer next() { 30 return st.pop().getInteger(); 31 } 32 33 @Override 34 public boolean hasNext() { 35 while (!st.isEmpty()) { 36 if (st.peek().isInteger()) return true; 37 List<NestedInteger> cur = st.pop().getList(); 38 for (int i=cur.size()-1; i>=0; i--) { 39 st.push(cur.get(i)); 40 } 41 } 42 return false; 43 } 44 } 45 46 /** 47 * Your NestedIterator object will be instantiated and called as such: 48 * NestedIterator i = new NestedIterator(nestedList); 49 * while (i.hasNext()) v[f()] = i.next(); 50 */
相关文章
- Java实现 LeetCode 707 设计链表(环形链表)
- Java实现 LeetCode 561 数组拆分 I(通过排序算法改写PS:难搞)
- Java实现 LeetCode 235 二叉搜索树的最近公共祖先
- Java实现LeetCode #986 - Interval List Intersections
- (LeetCode 203)Remove Linked List Elements
- [Algorithm] 234. Palindrome Linked List / Reverse linked list
- [LeetCode] Flatten Nested List Iterator
- [LeetCode] Odd Even Linked List
- [LeetCode] Reverse Linked List(递归与非递归反转链表)
- [LeetCode] Remove Linked List Elements
- json串 转 list<class> 方法 List转JSONArray和JSONArray转List String 转List
- 【LeetCode 37】解数独
- LeetCode_Lowest Common Ancestor of a Binary Search Tree (Binary Tree)
- Leetcode 984. 不含 AAA 或 BBB 的字符串(网友思路)
- Leetcode 628. 三个数的最大乘积
- Leetcode 1598. 文件夹操作日志搜集器
- [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
- leetcode Remove Duplicates from Sorted List
- LeetCode 122 Best Time to Buy and Sell Stock II
- leetcode - Convert Sorted List to Binary Search Tree
- leetcode 206. Reverse Linked List
- 【Leetcode刷题Python】从列表list中创建一颗二叉树