341. 扁平化嵌套列表迭代器
由于「栈」的先进后出的特性,我们需要逆序在栈里放入各个元素。
处理流程分为两步:
在构造函数中应该初始化,把当前列表的各个元素(不用摊平)逆序放入栈中。
在 hasNext() 方法中,访问(不弹出)栈顶元素,判断是否为 int:
如果是 int 那么说明有下一个元素,返回 true;然后 next() 就会被调用,把栈顶的 int 弹出;
如果是 list 需要把当前列表的各个元素(不用摊平)逆序放入栈中。
如果栈为空,那么说明原始的嵌套列表已经访问结束了,返回 false。
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* public:
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool isInteger() const;
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* int getInteger() const;
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* const vector<NestedInteger> &getList() const;
* };
*/
class NestedIterator {
public:
NestedIterator(vector<NestedInteger> &nestedList) {
for(int i = nestedList.size() - 1; i >= 0; i--){
st.push(nestedList[i]);
}
}
int next() {
NestedInteger cur = st.top();
st.pop();
return cur.getInteger();
}
bool hasNext() {
while(!st.empty()){
NestedInteger cur = st.top();
if(cur.isInteger()){
return true;
}
st.pop();
for(int i = cur.getList().size() - 1; i >= 0; i--){
st.push(cur.getList()[i]);
}
}
return false;
}
private:
stack<NestedInteger> st;
};
/**
* Your NestedIterator object will be instantiated and called as such:
* NestedIterator i(nestedList);
* while (i.hasNext()) cout << i.next();
*/
Java版本
java.util.ArrayDeque 类提供了可调整大小的阵列,并实现了Deque接口。以下是关于阵列双端队列的要点:
- 数组双端队列没有容量限制,使他们增长为必要支持使用。
- 它们不是线程安全的;如果没有外部同步。
- 不支持多线程并发访问。
- null元素被禁止使用在数组deques。
- 它们要比堆栈Stack和LinkedList快。
1 boolean add(E e)
此方法将添加指定的元素,在此deque队列的末尾。
2 void addFirst(E e)
此方法将添加指定的元素,在此deque队列的前面。
3 void addLast(E e)
此方法将插入指定的元素,在此deque队列的末尾。
4 void clear()
此方法移除此deque队列的元素。
5 ArrayDeque clone()
此方法返回此deque队列的副本。
6 boolean contains(Object o)
如果此deque 队列包含指定的元素,此方法返回true。
7 Iterator descendingIterator()
此方法返回一个迭代器在此deque队列以逆向顺序的元素。
8 E element()
此方法检索,但是不移除此deque队列表示的队列的头部。
9 E getFirst()
此方法检索,但是不移除此deque队列的第一个元素。
10 E getLast()
此方法检索,但是不移除此deque队列的最后一个元素。
11 boolean isEmpty()
如果此deque队列不包含元素,此方法返回true。
12 Iterator iterator()
此方法返回一个迭代器在此deque队列的元素。
13 boolean offer(E e)
此方法将指定的元素,在此deque队列的末尾。
14 boolean offerFirst(E e)
此方法将指定的元素,在此deque队列的前面。
15 boolean offerLast(E e)
此方法将指定的元素,在此deque队列的末尾。
16 E peek()
此方法检索,但是不移除此deque队列表示的队列的头部,如果此deque队列为空,则返回null。
17 E peekFirst()
此方法检索,但是不移除此deque 队列的第一个元素,或者如果此deque 队列为空,则返回null。
18 E peekLast()
此方法检索,但是不移除此deque队列的最后一个元素,如果此deque队列为空,则返回null。
19 E poll()
此方法检索并移除此deque队列表示的队列的头部,如果此deque队列为空,则返回null。
20 E pollFirst()
此方法检索并移除此deque队列的第一个元素,或者如果此deque队列为空,则返回null。
21 E pollLast()
此方法检索并移除此deque队列的最后一个元素,如果此deque队列为空,则返回null。
22 E pop()
这种方法的此deque队列所表示的堆栈弹出一个元素。
23 void push(E e)
这种方法将元素推入此deque队列所表示的堆栈。
24 E remove()
此方法检索并移除此deque队列表示的队列的头部。
25 boolean remove(Object o)
此方法从此deque队列中移除指定元素的单个实例。
26 E removeFirst()
此方法检索并移除此deque队列的第一个元素。
27 boolean removeFirstOccurrence(Object o)
此方法移除此deque队列的指定元素的第一个匹配。
28 E removeLast()
此方法检索并移除此deque队列的最后一个元素。
29 boolean removeLastOccurrence(Object o)
此方法移除此deque队列的指定元素的最后一次出现。
30 int size()
此方法返回在此deque队列的元素个数。
31 object[] toArray()
这个方法返回一个包含所有在此deque队列在适当的序列中元素的数组
/**
* // 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 Deque<NestedInteger> deque;
public NestedIterator(List<NestedInteger> nestedList) {
deque = new ArrayDeque<>();
for(int i = nestedList.size() - 1; i >= 0; i--){
deque.addLast(nestedList.get(i));
}
}
@Override
public Integer next() {
NestedInteger cur = deque.removeLast();
return cur.getInteger();
}
@Override
public boolean hasNext() {
while(!deque.isEmpty()){
NestedInteger top = deque.peekLast();
if(top.isInteger()) {
return true;
}
deque.removeLast();
for(int i = top.getList().size() - 1; i >= 0; i--){
deque.addLast(top.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();
*/