TransWikia.com

LeetCode 284: Peeking Iterator

Code Review Asked on November 13, 2021

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 {
        
    }
};

One Answer

  • Passing larger sized data in functions by const &data is a good idea since it does not make a copy. Note that when a parameter is passed by const&, the extra cost dereferencing and fewer opportunities for compile optimizing. You should do this typically when the data is large in size

  • From your next() function

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

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

        return curr_next;
    } ```

What is the use of the if (num_has_next) branch? You are returning a copy of next_num that was copied before any modifications, and since curr_next is not a reference of next_num, any changes to next_num will not effect return curr_next. I think what you might want to do is

int next(){
    next_num = (num_has_next) ? Iterator::next() : NULL;
    return next_num;
} 

ternary operators
Since @hjpotter92 has mentioned it, calling next() or peek() on the last element should return NULL.

Answered by Parekh on November 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP