225. stack implemented with queue leetcode

Problem Description

Queue implementation using the following stack:

push (x) - x elements onto the stack
pop () - remove the top of the stack
top () - Gets the top element
empty () - Returns the stack is empty
Note:

You can only use the basic operation of the queue - that is, push to back, peek / pop from front, size, and is empty of these operations are legal.
The language you are using may not support queue. You can use the list or the deque (deque) to simulate a queue, the queue as long as the operation to the standard.
You may assume that all operations are valid (for example, to an empty stack does not call the pop operation or top).

Reverse the sequence when the solution to a problem 1- stack

After using a queue, the queue element is added, before the reverse n-1 elements, the top element always remains in first team

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.my_queue=[]


    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.my_queue.append(x)
        for i in range(len(self.my_queue)-1):
            self.my_queue.append(self.my_queue.pop(0))



    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        return self.my_queue.pop(0)


    def top(self) -> int:
        """
        Get the top element.
        """
        return self.my_queue[0]


    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return  not self.my_queue



# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()

Press-time complexity O (n), the pop-O (1)
Here Insert Picture Description

The method of adjustment stack 2

Press-time complexity O (1), the pop-O (n)

class MyStack:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.queue1=[]
        self.queue2=[]
        self.top_element=0

        


    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.queue1.append(x)
        self.top_element=x



    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        item=0
        # 不加会一直报错local variable 'xxx' referenced before assignment
        #估计在for循环中只被当做for中局部变量
        for i in range(len(self.queue1)-1):
            item=self.queue1.pop(0)
            self.queue2.append(item)
        self.top_element=item
        target=self.queue1[0]
        self.queue1=self.queue2
        self.queue2=[]
        return target



    def top(self) -> int:
        """
        Get the top element.
        """
        return self.top_element


    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return self.queue1==[]

Here Insert Picture Description

Published 284 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_39289876/article/details/104745961