【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 ...
随机推荐
- 下载包含src,tgz,zip的文件
下载哪一个文件? 含src的是源码文件,含tgz和zip的分别是linux和windows系统下jar包(java文件包) asc,md5,sha1是三种加密文件,可用于判断文件是否被修改. Ente ...
- Python多行注释
由于Python的注释只有针对于单行的注释(用#),多行注释时很不方便(要想多行注释只能每行代码前面都加上#).在网上看到一个Python的多行注释方法,分享给大家,其实很简单的,就是使用一对三个双引 ...
- Python简明语法
- angularJs模糊查询
html代码 <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <tit ...
- token原理
token原理1.和session有很大关系哦. jsp生成表单时,在表单中插入一个隐藏<input>字段,该字段就是保存在页面端的token字符串,同时把该字符串存入session中.等 ...
- git-配置公司账号
1.初始化配置 git config --global user.name " abc" git config --global user.email "abc@123. ...
- nginx服务器状态监控
Nginx开启监控需在编译时加入with-http_stub_status_module,查看当前Nginx编译参数:/usr/local/nginx/sbin/nginx -V 1.以二级目录方式开 ...
- CSS之cssText
更改元素样式 <div style="width:100px;height:100px;text-align:center;line-height:100px;"> T ...
- angularJs之template指令
template: 如果我们只需要在ng-view 中插入简单的HTML 内容,则使用该参数: .when('/computers',{template:'这是电脑分类页面'}) templateUr ...
- LeetCode Reconstruct Itinerary
原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...