【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 ...
随机推荐
- 将公网IP自动发到Twitter上
[Twitter] 1. 在https://apps.twitter.com/创建新的应用 2. 在https://dev.twitter.com/rest/reference/post/status ...
- Final-阶段站立会议4
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- 分割excel sheet
Sub split_sheet() '输入用户想要拆分的工作表 Dim sheet_name sheet_name = Application.InputBox("请输入拆分工作表的名称:& ...
- 安卓仿微信Tab页用Fragment实现
最终效果图如: 实现步骤: 新建项目tabdemo,我选的是4.0.3版本,然后依次新建三个Fragment,名字分别为:ChatFragment.FriendFragment.FindFragmen ...
- iOS:Xcode8以下真机测试iOS10.0和iOS10.1配置包
一.介绍 xcode的升级都已经到8系列了,可是还是有很多开发者使用的xcode还是7系列,然而xcode7...最多支持9.3,无法给升级到10.0和10.1的iPhone手机用户进行真机测试.此时 ...
- Python之路-python(装饰器、生成器、迭代器、Json & pickle 数据序列化、软件目录结构规范)
装饰器: 首先来认识一下python函数, 定义:本质是函数(功能是装饰其它函数),为其它函数添加附件功能 原则: 1.不能修改被装饰的函数的源代码. 2.不 ...
- emoji探寻之路
emoji是什么? http://www.baike.com/wiki/emoji emoji表情符号,是20世纪90年代由NTT Docomo栗田穣崇(Shigetaka Kurit)创建的,词义来 ...
- c# 中几个关于string问题
1.string是一个应用类型,而不是值类型:为什么用起来很像值类型?因为微软对其做了特殊处理. 2. using System; namespace testForString { class Pr ...
- Leetcode: Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...
- cat常用参数详解
cat常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近,我的一个朋友对linux特别感兴趣,于是我觉得每天交给他一个命令的使用,这样一个月下来也会使用30个命令,基 ...