5
\$\begingroup\$

I'm posting my code for a LeetCode problem. If you'd like to review, please do so. Thank you for your time!

Problem

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 will be returned by the next call to next().

Example:

list: [1,2,3].

Call next() gets you 1, the first element in the list. Now you call
peek() and it returns 2, the next element. Calling next() after that
still return 2.  You call next() the final time and it returns 3, the
last element.  Calling hasNext() after that should return false. 

Follow up: How would you extend your design to be generic and work with all types, not just integer?

Code

#include <vector>


class PeekingIterator : public Iterator {
    int next_num;
    bool num_has_next;

public:
    PeekingIterator(const std::vector<int> &nums) : Iterator(nums) {
        num_has_next = Iterator::hasNext();

        if (num_has_next) {
            next_num = Iterator::next();
        }
    }

    int peek() const {
        return next_num;
    }

    int next() {
        int curr_next = next_num;
        num_has_next = Iterator::hasNext();

        if (num_has_next) {
            next_num = Iterator::next();
        }

        return curr_next;
    }

    bool hasNext() const {
        return num_has_next;
    }
};

Reference

LeetCode has a template for answering questions. There is usually a class named Solution with one or more public functions which we are not allowed to rename. For this question, the template is:

/*
 * 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;
 *      Iterator(const vector<int>& nums);
 *      Iterator(const Iterator& iter);
 *
 *      // 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() {
        
    }
    
    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    int next() {
        
    }
    
    bool hasNext() const {
        
    }
};
\$\endgroup\$
  • 1
    \$\begingroup\$ What if you peek from the last element? peek() after the 3 has been returned. In your case; it'd keep returning 3, but it should be null. \$\endgroup\$ – hjpotter92 Jul 17 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.