摘要:首先,根據迭代器需要不斷返回下一個元素,確定用堆棧來做。堆棧初始化數據結構,要先從后向前向堆棧壓入中的元素。在調用之前,先要用判斷下一個是還是,并進行的操作對要展開并順序壓入對直接返回。
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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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 ...
閱讀 3422·2023-04-25 22:44
閱讀 926·2021-11-15 11:37
閱讀 1632·2019-08-30 15:55
閱讀 2639·2019-08-30 15:54
閱讀 1080·2019-08-30 13:45
閱讀 1430·2019-08-29 17:14
閱讀 1853·2019-08-29 13:50
閱讀 3402·2019-08-26 11:39