[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();
    }
};

 

目录
相关文章
|
2月前
Iterator与ListIterator迭代器
Iterator与ListIterator迭代器
|
8天前
list转迭代器Iterator
list转迭代器Iterator
|
7月前
|
Java 索引
每日一道面试题之Iterator 和 ListIterator 有什么区别?
每日一道面试题之Iterator 和 ListIterator 有什么区别?
|
7月前
每日一道面试题之迭代器 Iterator 是什么?
每日一道面试题之迭代器 Iterator 是什么?
|
4月前
自己实现iterator迭代器
自己实现iterator迭代器
|
6月前
|
设计模式 Java Python
迭代器(Iterator)
迭代器(Iterator)是一种设计模式,用于遍历聚合对象的一种方法。迭代器提供了一种方法来访问聚合对象中的元素,而不需要暴露该对象的内部表示。迭代器模式在 Java 和 Python 等编程语言中广泛使用,可以用于处理列表、元组、集合等数据结构。
36 1
|
7月前
|
安全 Java
每日一道面试题之介绍一下Iterator
每日一道面试题之介绍一下Iterator
|
11月前
|
索引 容器
Iterator与ListIterator有什么区别
Iterator与ListIterator有什么区别
|
存储
LeetCode 284. Peeking Iterator
给定一个迭代器类的接口,接口包含两个方法: next() 和 hasNext()。设计并实现一个支持 peek() 操作的顶端迭代器 -- 其本质就是把原本应由 next() 方法返回的元素 peek() 出来。
49 0
LeetCode 284. Peeking Iterator
|
JavaScript 索引
简单理解遍历器Iterator
简单理解遍历器Iterator
90 0

热门文章

最新文章