Why is my Python solution behaving differently on my local machine?


  • 0
    A

    I know this implementation isn't super-efficient, so with that aside: locally, the results of hasNext are different from the judge's results and I have no idea why. Can anyone shed some light on it?

    import copy
    
    class PeekingIterator(object):
        def __init__(self, iterator):
            self.iter = iterator
    
        def peek(self):
            peekIter = copy.copy(self.iter)
            return next(peekIter)
    
        def next(self):
            return next(self.iter)
    
        def hasNext(self):
            peekIter = copy.copy(self.iter)
            return next(peekIter, None) is not None
    

    On input [1,2,3,4] with operations [0,1,1,2,2,1,1,2,0,1,0,2,0] (0: hasNext, 1: peek, 2: next), my local results match the expected results:
    ["true","1","1","1","2","3","3","3","true","4","true","4","false"]

    But the judge's output for my code is:
    ["true","1","1","1","2","3","3","3","true","4","true","4","true"]

    There's no static class state. It fails in exactly the same way when I perform a deep copy instead of a copy. I'm stumped.


Log in to reply
 

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