Simple C++ 1 line


  • 3
    S

    Use inherited copy constructor to get next without advancing iterator.
    Leave everything else alone for inheritance to take care of.

    class PeekingIterator : public Iterator
    {
    public:
    	PeekingIterator(const vector<int>& nums) : Iterator(nums) {}
    
    	int peek()
    	{
    	    return Iterator{*this}.next();
    	}
    };
    

Log in to reply
 

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