Any better solution of follow up (generic type)?


  • 0
    X

    Can anybody post his/her own solution for the follow up question?

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

    I have noticed most solutions simply used cache/buffer of size one and/or a boolean indicator. Clearly this solution does not meet the '''generic type''' constraint in the follow up question because you have to specify the type of cache/buffer.

    I have also saw a great solution without using cache/buffer. https://discuss.leetcode.com/topic/24909/simple-c-solution-1-line-per-method-without-extra-member-variables/2

    Apparently, it is a perfect answer of the follow up.
    The key point of this solution is the usage of copy constructor of base class Iterator::Iterator

        int peek()
        {
            return Iterator(*this).next();
        }
    
    

    One question remained unanswered here. What is inside the copy constructor? Is it deep copy or shallow copy?

    If it is deep copy, that would defeat the purpose of saving space in the original problem statement.

    What do you guys think of this solution? Any better solution for the follow up?


Log in to reply
 

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