成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Flatten Nested List Iterator

spacewander / 2495人閱讀

摘要:首先,根據(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.

Example

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].

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è)NestedIntegerInteger還是List,并進(jìn)行flatten的操作:對(duì)List要展開并順序壓入stack;對(duì)Integer直接返回true。這樣一來(lái),調(diào)用next()的時(shí)候,返回的就一定是Integer了。

Solution
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 Iterator {
    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();
    }
}
update 2018-11
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

相關(guān)文章

  • leetcode 341 Flatten Nested List Iterator 以及其他Iter

    摘要:返回的是表示是否走到了結(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, 指向的是下一...

    chaosx110 評(píng)論0 收藏0
  • leetcode341. Flatten Nested List Iterator

    摘要:題目要求假設(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...

    MartinHan 評(píng)論0 收藏0
  • LeetCode 341. Flatten Nested List Iterator

    摘要:設(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...

    mingzhong 評(píng)論0 收藏0
  • [LintCode/LeetCode] Flatten Binary Tree to Linked

    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 ...

    TNFE 評(píng)論0 收藏0
  • 【LC總結(jié)】Iterator題目<Zigzag 1&2><BST>&

    摘要:方法直接查找數(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 ...

    WelliJhon 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<