[LeetCode] 225. Implement Stack using Queues_Easy tag: Design
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Example:
MyStack stack = new MyStack(); stack.push(1);
stack.push(2);
stack.top(); // returns 2
stack.pop(); // returns 2
stack.empty(); // returns false
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
利用如下图所示的方式, 每次push的时候将queue里面的值都放到x的后面即可.
Push: O(n), others : O(1)
Code
class MyStack(object): def __init__(self):
"""
Initialize your data structure here.
"""
self.q = collections.deque() def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
size = len(self.q)
self.q.append(x)
while size:
self.q.append(self.q.popleft())
size -= 1 def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
return self.q.popleft() def top(self):
"""
Get the top element.
:rtype: int
"""
return self.q[0] def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return not self.q # Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
[LeetCode] 225. Implement Stack using Queues_Easy tag: Design的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- [LeetCode] 232. Implement Queue using Stacks_Easy tag: Design
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
随机推荐
- jQuery属性操作(四)
通过阅读jQuery为属性操作封装的基本方法和为处理兼容性问题提供的hooks,发现jQuery在属性操作方面并没有做过多的设计,只是处理一下兼容性问题,然后调用基础的DOM操作方法.以下是对JQue ...
- Android调试技巧
转自:http://gityuan.com/2017/07/11/android_debug/ 一. 获取Trace 调用栈信息(Trace)是分析异常经常使用的,这里简单划分两类情况: 当前线程Tr ...
- POJ 3579 Median(二分答案)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...
- [算法]Bobmer
package com.company; import com.sun.org.apache.bcel.internal.generic.AASTORE; import java.awt.*; imp ...
- like to do vs like doing
I like to eat apple 表示我喜欢吃苹果这种食物. I like eating apple 表示我喜欢吃苹果这种食物 或者 表示我喜欢吃苹果这个过程. like to do,表达的是倾 ...
- 8.21 js
2018-8-21 20:05:43 2018-8-21 20:56:30 明天再看!!!! 今天空闲多看了书 <百年孤独> <苏东坡传> 打印结果 shanghai js的 ...
- 360浏览器设置打开默认为chrome极速模式
<meta name="renderer" content="webkit"> 若页面需默认用ie兼容内核,增加标签: <meta name= ...
- PHP 学习笔记之一:thinkPHP的volist标签
Volist标签主要用于在模板中循环输出数据集或者多维数组. 属性: name : 必须,输出数据模板变量,后台提供的变量. id : 必须,是循环变量,可以随便定义,但是不能跟name相同. 举个栗 ...
- Linux系统下便捷使用中国知网的方式
https://blog.csdn.net/mowangajimide/article/details/54144379
- ZOJ 4029 - Now Loading!!! - [前缀和+二分]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4029 Time Limit: 1 Second Memory L ...