zoukankan      html  css  js  c++  java
  • 【leetcode❤python】 225. Implement Stack using Queues

    #-*- coding: UTF-8 -*-
    class Stack(object):
        def __init__(self):
            """
            initialize your data structure here.
            """
            self.inQueue=[]
            self.outQueue=[]

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

        def pop(self):
            """
            :rtype: nothing
            """
            i=0
            while i<len(self.inQueue)-1:
                self.outQueue.append(self.inQueue[i])
                i+=1
            self.inQueue=self.outQueue
            self.outQueue=[]
           
           
            
        def top(self):
            """
            :rtype: int
            """
            tmpQueue=self.inQueue
            i=0;
            while i<(len(self.inQueue)-1):
                self.outQueue.append(self.inQueue[i])
                i+=1
            
            res=[i for i in self.inQueue if i not in self.outQueue]
            self.outQueue=[]
            return res[0]
            

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

    sol=Stack()
    sol.push(1)
    sol.push(2)
    print sol.top()
    sol.pop()
    print sol.top()
    sol.pop()
    print sol.empty()

  • 相关阅读:
    移动端input中的placeholder属性垂直
    js将文字转化为语音并播放
    js生成二维码
    jquery移除事件,绑定事件,触发事件
    js计算本地时间
    正则判断支付金额
    去除input默认带的上下按钮与修改placeholder的默认颜色、背景、placeholder内容的大小
    rem自适应手机端布局
    Python_PyQt5_库
    Python_用PyQt5 建 notepad 界面
  • 原文地址:https://www.cnblogs.com/kwangeline/p/6059559.html
Copyright © 2011-2022 走看看