Python use only one list, correct?

  • -1
    class Queue:
        # initialize your data structure here.
        def __init__(self):
            self.queue = []
        # @param x, an integer
        # @return nothing
        def push(self, x):
        # @return nothing
        def pop(self):
        # @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:

    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

    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.