leetcode-pp / 91alg-6-daily-check

91 算法第六期打卡仓库
28 stars 0 forks source link

双栈 #9

Closed haixiaolu closed 2 years ago

haixiaolu commented 2 years ago

思路:

代码 / Python

`class MyQueue:

def __init__(self):

    """
    Initialize a stack and a storage

    """
    self.stack = []
    self.container = []

def push(self, x : int):

    """ Push element x to the end of the stack"""
    while self.stack:

        # put the element that popped from stack to storage
        self.container.append(self.stack.pop())
    self.container.append(x)

        # get the element that popped from storage to stack
    while self.container:
        self.stack.append(self.container.pop())

def pop(self):

    """" Removes the element from in front of queue and returns that element """
    return self.stack.pop()

def peek(self):

    """" Get the front element """
    return self.stack[-1]

def empty(self):

    """ check the queue is empty or not """

    return not bool (self.stack)`

时间复杂度