Python use only one list, correct?


  • -1
    S
    class Queue:
        # initialize your data structure here.
        def __init__(self):
            self.queue = []
        
        # @param x, an integer
        # @return nothing
        def push(self, x):
            self.queue.append(x)
        
        # @return nothing
        def pop(self):
            self.queue.pop(0)
    
        # @return an integer
        def peek(self):
            return self.queue[0]
    
        # @return an boolean
        def empty(self):
            return len(self.queue)==0
    

    The above code could be accepted.
    I notice the two stack solution here:
    https://leetcode.com/discuss/44106/short-o-1-amortized

    Since I am not familiar with Java, I just wonder is it necessary to use two stacks in Python?
    It appears to me that there's no advantages of using two stacks in Python.


  • 2

    You don't use the list as a stack, you use it as a queue. That's forbidden. Depending on how you look at it, either your push violates the restriction or your pop and peek do.


  • 0
    S

    Yeah, I see. Thanks so much!


Log in to reply
 

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