国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Flatten Nested List Iterator

spacewander / 2483人閱讀

摘要:首先,根據迭代器需要不斷返回下一個元素,確定用堆棧來做。堆棧初始化數據結構,要先從后向前向堆棧壓入中的元素。在調用之前,先要用判斷下一個是還是,并進行的操作對要展開并順序壓入對直接返回。

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

這個迭代器要實現三個功能:
初始化結構,找下一個元素,判斷是否存在下一個元素。
首先,根據迭代器需要不斷返回下一個元素,確定用堆棧來做。
堆棧初始化數據結構,要先從后向前向堆棧壓入nestedList中的元素。
在調用next()之前,先要用hasNext()判斷下一個NestedIntegerInteger還是List,并進行flatten的操作:對List要展開并順序壓入stack;對Integer直接返回true。這樣一來,調用next()的時候,返回的就一定是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;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65990.html

相關文章

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

    摘要:返回的是表示是否走到了結尾。起到的就是緩存作用,因為調用之后馬上就走到下一個了。如果調用,返回用得到和最初的輸入相同的做相同的步驟存入不斷拆開得到結果。思想就是來自括號,后面也會跟進的專題 Iterator其實就是一個單鏈表,無法回頭看。java里很多數據結構都有這個接口,使用時需要initalize,得到一個iterator. 調用next()返回的是一個object, 指向的是下一...

    chaosx110 評論0 收藏0
  • leetcode341. Flatten Nested List Iterator

    摘要:題目要求假設有一個嵌套形式的數組,要求按照順序遍歷數組中的元素。思路和代碼首先可以想到通過深度優先遞歸的方式將嵌套形式的數組展開為一個無嵌套的列表。 題目要求 Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a lis...

    MartinHan 評論0 收藏0
  • LeetCode 341. Flatten Nested List Iterator

    摘要:設計一個迭代器,使其能夠遍歷這個整型列表中的所有整數。列表中的項或者為一個整數,或者是另一個列表。示例輸入輸出解釋通過重復調用直到返回,返回的元素的順序應該是。 Description Given a nested list of integers, implement an iterator to flatten it. Each element is either an integ...

    mingzhong 評論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 評論0 收藏0
  • 【LC總結】Iterator題目<Zigzag 1&2><BST>&

    摘要:方法直接查找數組的位的迭代器,調用方法得到的整數即為要返回的元素。再寫迭代器的方法返回指針元素的并讓指針通過遞歸方法指向下一個元素。 Zigzag Iterator Problem Given two 1d vectors, implement an iterator to return their elements alternately. Example Given two 1d ...

    WelliJhon 評論0 收藏0

發表評論

0條評論

spacewander

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<