Sunday, August 30, 2015

Leetcode 232. Implement Queue using Stacks

https://leetcode.com/problems/implement-queue-using-stacks/

Solution:
# T:O(1) S:O(n)
class Queue:
    # initialize your data structure here.
    def __init__(self):
        self.A, self.B = [], []

    # @param x, an integer
    # @return nothing
    def push(self, x):
        self.A.append(x)

    # @return nothing
    def pop(self):
        self.peek()
        self.B.pop()
        
    # @return an integer
    def peek(self):
        if not self.B:
            while self.A:
                self.B.append(self.A.pop())
        return self.B[-1]
        
    # @return an boolean
    def empty(self):
        return not self.A and not self.B
Run Time: 40 ms

No comments:

Post a Comment