@@ -0,0 +1,2849 @@
+%22%22%22%0AQuestion:%0A    Implement Queue using Stacks%0A%0A    Implement the following operations of a queue using stacks.%0A%0A    push(x) -- Push element x to the back of queue.%0A    pop() -- Removes the element from in front of queue.%0A    peek() -- Get the front element.%0A    empty() -- Return whether the queue is empty.%0A%0A    Notes:%0A    You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.%0A    Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.%0A    You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).%0A%0APerformance:%0A    1. Total Accepted: 18785 Total Submissions: 55431 Difficulty: Easy%0A    2. Your runtime beats 21.35%25 of python submissions.%0A%0ADesign note:%0A    At first time, it's hard to think that Queue and Stack are absolutely two different thing.%0A%0A    How to get the first element in a stack?%0A%0A    Well, we must use another storage to store the poped items, Yes, add another stack!%0A%0A    Of course, it's weird. After check discussion of leetcode, it's valid.%0A%22%22%22%0A%0Aclass Stack(object):%0A%0A    def __init__(self):%0A        self.stack = list()%0A%0A    def push(self, x):%0A        return self.stack.append(x)%0A%0A    def pop(self):%0A        return self.stack.pop()%0A%0A    def size(self):%0A        return len(self.stack)%0A%0A    def empty(self):%0A        return self.size() == 0%0A%0A%0Aclass Queue(object):%0A    def __init__(self):%0A        %22%22%22%0A        initialize your data structure here.%0A        %22%22%22%0A        self.left_stack = Stack()  # master%0A        self.right_stack = Stack()  # slave%0A%0A    def push(self, x):%0A        %22%22%22%0A        :type x: int%0A        :rtype: nothing%0A        %22%22%22%0A        self.left_stack.push(x)%0A%0A    def pop(self):%0A        %22%22%22%0A        :rtype: nothing%0A        %22%22%22%0A        return self.pop_peek_common(True)%0A%0A    def peek(self):%0A        %22%22%22%0A        :rtype: int%0A        %22%22%22%0A        return self.pop_peek_common(False)%0A%0A    def empty(self):%0A        %22%22%22%0A        :rtype: bool%0A        %22%22%22%0A        return self.left_stack.empty()%0A%0A    def pop_peek_common(self, should_delete):%0A        while self.left_stack.size() %3E 0:%0A            item = self.left_stack.pop()%0A            self.right_stack.push(item)%0A%0A        the_item = self.right_stack.pop()%0A        if not should_delete:%0A            self.left_stack.push(item)%0A%0A        while not self.right_stack.empty():%0A            item = self.right_stack.pop()%0A            self.left_stack.push(item)%0A%0A        return the_item%0A%0Aq = Queue()%0Aassert q.empty() is True%0Aq.push(1)%0Aassert q.peek() == 1%0Aassert q.empty() is False%0Aassert q.pop() == 1%0Aassert q.empty() is True%0Aq.push(2)%0Aq.push(3)%0Aq.push(4)%0Aassert q.empty() is False%0Aassert q.peek() == 2%0A

Add 232-implement-queue-using-stacks.py