LeetCode 341. Flatten Nested List Iterator

本文介绍了一种解决LeetCode上Flatten Nested List Iterator问题的方法,通过使用栈来实现迭代器的next和hasNext操作。具体地,采用从后向前的方式将nestedList的元素存入栈中,并在每次调用hasNext时检查栈顶元素是否为整数,如果不是则将其展开并重新存入栈中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接在这里:https://leetcode.com/problems/flatten-nested-list-iterator/?tab=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 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<NestedInteger> stk 从后向前存储nestedList的每个item. hasNext() 拿出stk顶部item, 若不是Integer就把该item从后向前放回stk中.

Time Complexity: next, O(1). hasNext, O(n).

Space: O(n).

AC Java:

 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     Stack<NestedInteger> stk;
21     public NestedIterator(List<NestedInteger> nestedList) {
22         stk = new Stack<NestedInteger>();
23         for(int i = nestedList.size()-1; i>=0; i--){
24             stk.push(nestedList.get(i));
25         }
26     }
27 
28     @Override
29     public Integer next() {
30         return stk.pop().getInteger();
31     }
32 
33     @Override
34     public boolean hasNext() {
35         while(!stk.isEmpty()){
36             NestedInteger cur = stk.peek();
37             if(cur.isInteger()){
38                 return true;
39             }
40             
41             stk.pop();
42             for(int i = cur.getList().size()-1; i>=0; i--){
43                 stk.push(cur.getList().get(i));
44             }
45         }
46         return false;
47     }
48 }
49 
50 /**
51  * Your NestedIterator object will be instantiated and called as such:
52  * NestedIterator i = new NestedIterator(nestedList);
53  * while (i.hasNext()) v[f()] = i.next();
54  */

类似Nested List Weight SumZigzag IteratorMini ParserFlatten 2D Vector.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/6440849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值