zoukankan      html  css  js  c++  java
  • 【leetcode❤python】232. Implement Queue using Stacks

    #-*- coding: UTF-8 -*-
    #双栈法
    class Queue(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.inStack=[]
            self.outStack=[]
            

        def push(self, x):
            """
            :type x: int
            :rtype: nothing
            """
            self.inStack.append(x)
            

        def pop(self):
            """
            :rtype: nothing
            """
            self.peek()
            self.outStack.pop()
            

        def peek(self):
            """
            :rtype: int
            """
            if not self.outStack:
                while self.inStack:
                    self.outStack.append(self.inStack.pop())
            return self.outStack[-1]
            

        def empty(self):
            """
            :rtype: bool
            """
            return True if (len(self.inStack)+len(self.outStack))==0 else False

  • 相关阅读:
    REST
    Bootstrap
    深入浅出聊聊企业级API网关
    Message Queue
    pyspark
    贝叶斯解读
    Leetcode#95 Unique Binary Search Trees II
    Leetcode#24 Swap Nodes in Pairs
    Leetcode#147 Insertion Sort List
    Leetcode#98 Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/kwangeline/p/5953582.html
Copyright © 2011-2022 走看看