【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()
【leetcode❤python】 225. Implement Stack using Queues的更多相关文章
- 【LeetCode】225. Implement Stack using Queues 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- 【LeetCode】225. Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- 【leetcode❤python】 155. Min Stack
#-*- coding: UTF-8 -*- class MinStack(object): def __init__(self): """ ...
- 【easy】225. Implement Stack using Queues
用队列实现栈.这个实现方法十分的简单,就是在push这一步的时候直接变成逆序. class MyStack { private: queue<int> q; queue<int> ...
- 【leetcode❤python】 28. Implement strStr()
#-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object): def strStr(se ...
- 【leetcode❤python】232. Implement Queue using Stacks
#-*- coding: UTF-8 -*-#双栈法class Queue(object): def __init__(self): """ ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- Ruby--strftime
-- [17 Oct 2016 05:37:18AM]:%e %b %Y %H:%M:%S%p -- [Oct 17, 2016]:%b %d, %Y -- [10-17-16 05:37]:%m-% ...
- css页面布局基础
1.盒模型属性包括boder.margin.padding.width.height,这些属性可以使用快捷方式表示,顺序为上右下左,值之间用空格隔开. 2.使用clip和overflow属性时,pos ...
- 修改ArcGIS Server Account / 站点管理员的 账号、密码
安装ArcGIS Server(10.1之后版本)时会先后创建两个账户:ArcGIS Server Account和站点管理账户 ArcGIS Server Account 是操作系统账户,Serve ...
- iOS:高德地图的使用
本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示.地图的绘制.地图的定位.地图的POI数据检索.地图的线路规划.地图导航等下载地址如下:https://github.com/xiayua ...
- Mac系统上用Node做APNS
1.安装Node,下载地址:http://nodejs.org 2.更新npm,终端命令:sudo npm update npm -g 3.安装apn,终端命令:npm install apn 4.导 ...
- Vcenter server 5.5克隆模板(创建ISO镜像)
1.进入Vcenter server 5.5控制台 --- 选择虚拟机和模版. 2.右键 XP_32 --- 模版 ---- 克隆为模板(Vcenter server 5.5连接的VMware ESX ...
- 产生0-9 A-Z a-z
>题目要求: >>产生26个大写字母 >>产生26个小写字母 >>产生0-9这10个阿拉伯数字 >程序实现: package cn.fury.test; ...
- kafka 生产者java编码
public class KafkaProducerDemo { public static void main(String[] args) throws InterruptedException ...
- leetcode105:Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- SQL语法粗整理
1.在同一张表中,对前一条数据进行更新性插入操作,即: