[LeetCode] Peeking Iterator

简介: An interesting problem about iterators. This post shares a very nice solution, which is rewritten below, with minor simplifications.

An interesting problem about iterators. This post shares a very nice solution, which is rewritten below, with minor simplifications.

// Below is the interface for Iterator, which is already defined for you.
// **DO NOT** modify the interface for Iterator.
class Iterator {
    struct Data;
    Data* data;
public:
    Iterator(const vector<int>& nums);
    Iterator(const Iterator& iter);
    virtual ~Iterator();
    // Returns the next element in the iteration.
    int next();
    // Returns true if the iteration has more elements.
    bool hasNext() const;
};


class PeekingIterator : public Iterator {
public:
    PeekingIterator(const vector<int>& nums) : Iterator(nums) {
        // Initialize any member here.
        // **DO NOT** save a copy of nums and manipulate it directly.
        // You should only use the Iterator interface methods.
        peeked = false;
    }

    // Returns the next element in the iteration without advancing the iterator.
    int peek() {
        if (!peeked) {
            peeked = true;
            peekElem = Iterator::next();
        }
        return peekElem;
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    int next() {
        if(peeked) {
            peeked = false;
            return peekElem;
        }
        return Iterator::next();
    }

    bool hasNext() const {
        return peeked || Iterator::hasNext();
    }
private:
    bool peeked;
    int peekElem;
};

BTW, it seems that we tend to misspell peek to peak. Well, after learning what peek wants to do, I understood why it uses such a name: peek means to look furtively, which is just like what peek does compared to next :-)

This post shares another super concise solution, just 3 lines of added code in total! And I just know too few about classes in C++ to come up with it...

// Below is the interface for Iterator, which is already defined for you.
// **DO NOT** modify the interface for Iterator.
class Iterator {
    struct Data;
    Data* data;
public:
    Iterator(const vector<int>& nums);
    Iterator(const Iterator& iter);
    virtual ~Iterator();
    // Returns the next element in the iteration.
    int next();
    // Returns true if the iteration has more elements.
    bool hasNext() const;
};


class PeekingIterator : public Iterator {
public:
    PeekingIterator(const vector<int>& nums) : Iterator(nums) {
        // Initialize any member here.
        // **DO NOT** save a copy of nums and manipulate it directly.
        // You should only use the Iterator interface methods.
        
    }

    // Returns the next element in the iteration without advancing the iterator.
    int peek() {
        return Iterator(*this).next();
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    int next() {
        return Iterator::next();
    }

    bool hasNext() const {
        return Iterator::hasNext();
    }
};

 

目录
相关文章
LeetCode 341. Flatten Nested List Iterator
给定一个嵌套的整型列表。设计一个迭代器,使其能够遍历这个整型列表中的所有整数。 列表中的项或者为一个整数,或者是另一个列表。
71 0
LeetCode 341. Flatten Nested List Iterator
|
存储
LeetCode 284. Peeking Iterator
给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。
85 0
LeetCode 284. Peeking Iterator
[LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.
1255 0
[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
1763 0
|
Java API C++
[LeetCode] Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a PeekingIterator that support the peek() operation – it essentially peek() at the element that wi
976 0
[LeetCode] Zigzag Iterator
Problem Description: Given two 1d vectors, implement an iterator to return their elements alternately.
802 0