【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 ...
随机推荐
- 使用engine关键字指定该表使用哪个engine
建表及插入数据语句:mysql> create table salary(userid int,salary decimal(9,2));Query OK, 0 rows affected (0 ...
- 在VPS上部署fq环境
VPS购买地址 1. 由于我选择的是CentOS 6 x86版本, 需要安装如下准备工具: git, gcc-c++, zlib-devel, openssl-devel, pcre-devel 2. ...
- Java的析构函数System的finalize()
一个对象是由产生 到使用 到销毁的过程 即C++中 构造函数-> body->析构函数 在Java之中为了回收不需要的空间可以使用System类的finalize() class A{ p ...
- Android WebApp开发使用Genymotion连接Fiddler2/Charles代理调试
1. 目的 在模拟器的浏览器或app hybrid开发中遇到chrome调试代码为线上代码或者混淆代码时,可以利用fiddler/charles为genymotion配置代理, 可以方便的 ...
- NSString(或者说是UILabel)加入 “行间距” 之后的 “高度”计算
一.首先,写一个工具类(NSString的分类,增加两个功能,计算高度宽度) #import "NSString+Extension.h" @implementation NSSt ...
- MyBatis操作指南-搭建项目基础环境(基于XML)含log4j配置
- Java学习——增强for循环、switch
增强for循环 Java5引入了一种主要用于数组的增强型for循环. 例子 public class ForTest { public static void main(String args[]){ ...
- [Android Tips] 2. Disable recent apps dialog on long press home button
public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); Log.d(&qu ...
- 《30天自制操作系统》12_day_学习笔记
harib09a: 定时器:(Timer)每隔一段时间,会向CPU发送一个中断.这样CPU不用记住每一条指令的执行时间.没有定时器很多指令CPU都很难执行.例如HLT指令,这个指令的执行时间不是个固定 ...
- php组成数组
每次向数据库取5条数据,不足5条就不显示 $z = (int)(count($data) / 5);for ($ii = 1; $ii <= $z; $ii++) { foreach ($dat ...