摘要:首先,根据迭代器需要不断返回下一个元素,确定用堆栈来做。堆栈初始化数据结构,要先从后向前向堆栈压入中的元素。在调用之前,先要用判断下一个是还是,并进行的操作对要展开并顺序压入对直接返回。
Problem
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.
ExampleGiven 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].
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].
Note这个迭代器要实现三个功能:
初始化结构,找下一个元素,判断是否存在下一个元素。
首先,根据迭代器需要不断返回下一个元素,确定用堆栈来做。
堆栈初始化数据结构,要先从后向前向堆栈压入nestedList中的元素。
在调用next()之前,先要用hasNext()判断下一个NestedInteger是Integer还是List
import java.util.Iterator; public class NestedIterator implements Iterator{ Stack stack = new Stack(); public NestedIterator(List nestedList) { for (int i = nestedList.size()-1; i >= 0; i--) { stack.push(nestedList.get(i)); } } @Override public Integer next() { return stack.pop().getInteger(); } @Override public boolean hasNext() { while (!stack.isEmpty()) { NestedInteger cur = stack.peek(); if (cur.isInteger()) return true; stack.pop(); for (int i = cur.getList().size()-1; i >= 0; i--) stack.push(cur.getList().get(i)); } return false; } }
By using internalNext()
public class NestedIterator implements Iterator{ private NestedInteger peek = null; private Iterator iterator; private Stack > stack = new Stack(); public NestedIterator(List nestedList) { iterator = nestedList.iterator(); peek = internalNext(); } private NestedInteger internalNext() { if (iterator.hasNext()) { NestedInteger i = iterator.next(); if (i.isInteger()) return i; else { stack.add(iterator); iterator = i.getList().iterator(); return internalNext(); } } else if (stack.size() > 0) { iterator = stack.pop(); return internalNext(); } else return null; } @Override public Integer next() { Integer next = peek.getInteger(); peek = internalNext(); return next; } @Override public boolean hasNext() { return peek != null; } }
A much much better method
public class NestedIterator implements Iteratorupdate 2018-11{ private List flatList; private int index; public NestedIterator(List nestedList) { flatList = new ArrayList<>(); flatten(nestedList); } public void flatten(List nestedList) { for (NestedInteger i: nestedList) { if (i.isInteger()) flatList.add(i.getInteger()); else flatten(i.getList()); } } @Override public Integer next() { return hasNext ? flatList.get(index++) : null; } @Override public boolean hasNext() { return index < flatList.size(); } }
public class NestedIterator implements Iterator{ Deque stack; public NestedIterator(List nestedList) { stack = new ArrayDeque<>(); for (int i = nestedList.size()-1; i >= 0; i--) { stack.push(nestedList.get(i)); } } @Override public Integer next() { if (hasNext()) { return stack.pop().getInteger(); } return null; } @Override public boolean hasNext() { while (!stack.isEmpty()) { NestedInteger cur = stack.peek(); if (cur.isInteger()) return true; else { stack.pop(); List list = cur.getList(); for (int i = list.size()-1; i >= 0; i--) { stack.push(list.get(i)); } } } return false; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65990.html
摘要:返回的是表示是否走到了结尾。起到的就是缓存作用,因为调用之后马上就走到下一个了。如果调用,返回用得到和最初的输入相同的做相同的步骤存入不断拆开得到结果。思想就是来自括号,后面也会跟进的专题 Iterator其实就是一个单链表,无法回头看。java里很多数据结构都有这个接口,使用时需要initalize,得到一个iterator. 调用next()返回的是一个object, 指向的是下一...
摘要:题目要求假设有一个嵌套形式的数组,要求按照顺序遍历数组中的元素。思路和代码首先可以想到通过深度优先递归的方式将嵌套形式的数组展开为一个无嵌套的列表。 题目要求 Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a lis...
摘要:设计一个迭代器,使其能够遍历这个整型列表中的所有整数。列表中的项或者为一个整数,或者是另一个列表。示例输入输出解释通过重复调用直到返回,返回的元素的顺序应该是。 Description Given a nested list of integers, implement an iterator to flatten it. Each element is either an integ...
Problem Flatten a binary tree to a fake linked list in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode. Example 1 1 ...
摘要:方法直接查找数组的位的迭代器,调用方法得到的整数即为要返回的元素。再写迭代器的方法返回指针元素的并让指针通过递归方法指向下一个元素。 Zigzag Iterator Problem Given two 1d vectors, implement an iterator to return their elements alternately. Example Given two 1d ...
阅读 3410·2023-04-25 22:44
阅读 905·2021-11-15 11:37
阅读 1616·2019-08-30 15:55
阅读 2602·2019-08-30 15:54
阅读 1067·2019-08-30 13:45
阅读 1416·2019-08-29 17:14
阅读 1838·2019-08-29 13:50
阅读 3373·2019-08-26 11:39