A simple C++ solution


  • 5
    C
    // 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:
        bool peaked;
        int peakedElement;
    	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.
    	    peaked = false;
    	}
    
        // Returns the next element in the iteration without advancing the iterator.
    	int peek() 
    	{
            if(!peaked && Iterator::hasNext())
            {
                peaked = true;
                peakedElement = Iterator::next();
                return peakedElement;
            }
            else if(peaked)
            {
                return peakedElement;
            }
    	}
    
    	// hasNext() and next() should behave the same as in the Iterator interface.
    	// Override them if needed.
    	int next() 
    	{
    	    if(peaked)
    	    {
    	        peaked = false;
    	        return peakedElement;
    	    }
    	    if(Iterator::hasNext())
    	        return Iterator::next();
    	}
    
    	bool hasNext() const 
    	{
    	    if(peaked)
    	        return true;
    	   return Iterator::hasNext();
    	}
    };

  • 1

    Hi, arindamkhaled. Thanks for your sharing. Your code is really nice!

    In fact, the logic of peek may be simplified a little bit to:

    int peek() {
        if(!peaked) {
            peaked = true;
            peakedElement = Iterator::next();
        }
        return peakedElement;
    }
    

    And hasNext can be done simply by:

    return peaked || Iterator::hasNext();
    

    BTW, it seems that you misspell peek to peak. Well, I also did that at the first glance. 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 :-)


  • 0
    C

    Thanks jianchao.li.fighter. I will try to get them corrected soon.


  • 0
    L

    Hi, I'm a little confused why you can use Iterator like that? Iterator was inited in PeekingIterator's constructor but it didn't defined as an data member. If it's a abstract interface, at least we should have a pure virtual function? Can you clarify this? Thanks!


  • 0
    C

    Hi luantian, I'm I don't understand your question. Can you please elaborate more if you are still interested in knowing the answer?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.