摘要:首先,根據(jù)迭代器需要不斷返回下一個(gè)元素,確定用堆棧來(lái)做。堆棧初始化數(shù)據(jù)結(jié)構(gòu),要先從后向前向堆棧壓入中的元素。在調(diào)用之前,先要用判斷下一個(gè)是還是,并進(jìn)行的操作對(duì)要展開并順序壓入對(duì)直接返回。
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這個(gè)迭代器要實(shí)現(xiàn)三個(gè)功能:
初始化結(jié)構(gòu),找下一個(gè)元素,判斷是否存在下一個(gè)元素。
首先,根據(jù)迭代器需要不斷返回下一個(gè)元素,確定用堆棧來(lái)做。
堆棧初始化數(shù)據(jù)結(jié)構(gòu),要先從后向前向堆棧壓入nestedList中的元素。
在調(diào)用next()之前,先要用hasNext()判斷下一個(gè)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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65990.html
摘要:返回的是表示是否走到了結(jié)尾。起到的就是緩存作用,因?yàn)檎{(diào)用之后馬上就走到下一個(gè)了。如果調(diào)用,返回用得到和最初的輸入相同的做相同的步驟存入不斷拆開得到結(jié)果。思想就是來(lái)自括號(hào),后面也會(huì)跟進(jìn)的專題 Iterator其實(shí)就是一個(gè)單鏈表,無(wú)法回頭看。java里很多數(shù)據(jù)結(jié)構(gòu)都有這個(gè)接口,使用時(shí)需要initalize,得到一個(gè)iterator. 調(diào)用next()返回的是一個(gè)object, 指向的是下一...
摘要:題目要求假設(shè)有一個(gè)嵌套形式的數(shù)組,要求按照順序遍歷數(shù)組中的元素。思路和代碼首先可以想到通過(guò)深度優(yōu)先遞歸的方式將嵌套形式的數(shù)組展開為一個(gè)無(wú)嵌套的列表。 題目要求 Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a lis...
摘要:設(shè)計(jì)一個(gè)迭代器,使其能夠遍歷這個(gè)整型列表中的所有整數(shù)。列表中的項(xiàng)或者為一個(gè)整數(shù),或者是另一個(gè)列表。示例輸入輸出解釋通過(guò)重復(fù)調(diào)用直到返回,返回的元素的順序應(yīng)該是。 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 ...
摘要:方法直接查找數(shù)組的位的迭代器,調(diào)用方法得到的整數(shù)即為要返回的元素。再寫迭代器的方法返回指針元素的并讓指針通過(guò)遞歸方法指向下一個(gè)元素。 Zigzag Iterator Problem Given two 1d vectors, implement an iterator to return their elements alternately. Example Given two 1d ...
閱讀 3436·2023-04-25 22:44
閱讀 949·2021-11-15 11:37
閱讀 1644·2019-08-30 15:55
閱讀 2658·2019-08-30 15:54
閱讀 1096·2019-08-30 13:45
閱讀 1443·2019-08-29 17:14
閱讀 1866·2019-08-29 13:50
閱讀 3424·2019-08-26 11:39