[LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器

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

LeetCode上的原题,请参见我之前的博客Flatten Nested List Iterator。但是不太明白的是,那篇博客中的解法三可以通过LeetCode的OJ,在LintCode上跑就有错误,不知道啥原因。

解法一:

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (int i = nestedList.size() - 1; i >= 0; --i) {
            s.push(nestedList[i]);
        }
    }

    int next() {
        NestedInteger t = s.top(); s.pop();
        return t.getInteger();
    }

    bool hasNext() {
        while (!s.empty()) {
            NestedInteger t = s.top();
            if (t.isInteger()) return true;
            s.pop();
            for (int i = t.getList().size() - 1; i >= 0; --i) {
                s.push(t.getList()[i]);
            }
        }
        return false;
    }

private:
    stack<NestedInteger> s;
};

解法二:

class NestedIterator {
public:
    NestedIterator(vector<NestedInteger> &nestedList) {
        for (auto a : nestedList) {
            d.push_back(a);
        }
    }

    int next() {
        NestedInteger t = d.front(); d.pop_front();
        return t.getInteger();
    }

    bool hasNext() {
        while (!d.empty()) {
            NestedInteger t = d.front();
            if (t.isInteger()) return true;
            d.pop_front();
            for (int i = 0; i < t.getList().size(); ++i) {
                d.insert(d.begin() + i, t.getList()[i]);
            }
        }
        return false;
    }

private:
    deque<NestedInteger> d;
};
时间: 2024-11-09 15:57:50

[LintCode] Flatten Nested List Iterator 压平嵌套链表迭代器的相关文章

[LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

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 1: Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasN

[leetcode]341. Flatten Nested List Iterator 展开嵌套列表的迭代器

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 1: Input: [[1,1],2,[1,1]] Output: [1,1,2,1,1] Explanation: By calling ne

[LeetCode][Python]Flatten Nested List Iterator

Flatten Nested List Iterator 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 1:Given the list [[1,1],2,[1,1]], By callin

Flatten Nested List Iterator

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. Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasNext returns

341. Flatten Nested List Iterator

https://leetcode.com/problems/flatten-nested-list-iterator/#/description 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

leetcode 341. Flatten Nested List Iterator

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 1: Given the list [[1,1],2,[1,1]], By calling next repeatedly until hasN

341. Flatten Nested List Iterator展开多层数组

[抄题]: 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 1:Given the list [[1,1],2,[1,1]], By calling next repeatedly until

LeetCode &quot;Flatten Nested List Iterator&quot;

Besides the common stack-based solutions, here is another recursive NestedIterator solution :) class NestedIterator { int currInx; const vector<NestedInteger> &rData; NestedIterator *sub; void move() { if (currInx < 0 || rData[currInx].isInte

[Swift]LeetCode339. 嵌套链表权重和 $ Nested List Weight Sum

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Example 1:Given the list [[1,1],2,[1,1]], return