One-Liners for Fun, C++


  • 0

    Not good code, I just wanted to see whether I can and how it would look. But I like the word "peekee", modeled it after "employee", "addressee", etc.

    class PeekingIterator : public Iterator {
        int peekee, peeked = 0;
    public:
    	PeekingIterator(const vector<int>& nums) : Iterator(nums) {
    	}
    
    	int peek() {
            return peeked++ ? peekee : peekee = Iterator::next();
    	}
    
    	int next() {
    	    return peeked ? (peeked = 0) + peekee : Iterator::next();
    	}
    
    	bool hasNext() const {
    	    return peeked || Iterator::hasNext();
    	}
    };

  • 0

    There would be a possibility of overflow if only peek() was called and called for eternity.
    Just a joke :-)


  • 0

    Yeah, that's one reason it's not good code :-)


Log in to reply
 

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