[LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Notes:
- You must use only standard operations of a stack -- which means only
push to top,peek/pop from top,size, andis emptyoperations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
用栈来实现队列,对比类似题目225. Implement Stack using Queues是反过来用。
Java:
class MyQueue {
Stack<Integer> temp = new Stack<Integer>();
Stack<Integer> value = new Stack<Integer>();
// Push element x to the back of queue.
public void push(int x) {
if(value.isEmpty()){
value.push(x);
}else{
while(!value.isEmpty()){
temp.push(value.pop());
}
value.push(x);
while(!temp.isEmpty()){
value.push(temp.pop());
}
}
}
// Removes the element from in front of queue.
public void pop() {
value.pop();
}
// Get the front element.
public int peek() {
return value.peek();
}
// Return whether the queue is empty.
public boolean empty() {
return value.isEmpty();
}
}
Python:
class Queue:
# initialize your data structure here.
def __init__(self):
self.A, self.B = [], [] # @param x, an integer
# @return nothing
def push(self, x):
self.A.append(x) # @return an integer
def pop(self):
self.peek()
return self.B.pop() # @return an integer
def peek(self):
if not self.B:
while self.A:
self.B.append(self.A.pop())
return self.B[-1] # @return an boolean
def empty(self):
return not self.A and not self.B
C++:
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
stack<int> tmp;
while (!s.empty()) {
tmp.push(s.top());
s.pop();
}
s.push(x);
while (!tmp.empty()) {
s.push(tmp.top());
tmp.pop();
}
}
// Removes the element from in front of queue.
void pop(void) {
s.pop();
}
// Get the front element.
int peek(void) {
return s.top();
}
// Return whether the queue is empty.
bool empty(void) {
return s.empty();
}
private:
stack<int> s;
};
C++:
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
_new.push(x);
}
void shiftStack() {
if (_old.empty()) {
while (!_new.empty()) {
_old.push(_new.top());
_new.pop();
}
}
}
// Removes the element from in front of queue.
void pop(void) {
shiftStack();
if (!_old.empty()) _old.pop();
}
// Get the front element.
int peek(void) {
shiftStack();
if (!_old.empty()) return _old.top();
return 0;
}
// Return whether the queue is empty.
bool empty(void) {
return _old.empty() && _new.empty();
}
private:
stack<int> _old, _new;
};
类似题目:
[LeetCode] 225. Implement Stack using Queues 用队列来实现栈
All LeetCode Questions List 题目汇总
[LeetCode] 232. Implement Queue using Stacks 用栈来实现队列的更多相关文章
- 232 Implement Queue using Stacks 用栈来实现队列
使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
随机推荐
- 【Calling Circles UVA - 247 】【Floyd + dfs】
用到的东西 Floyd算法(不考虑路径的长度,只关心两点之间是否有通路,可用于求有向图的传递闭包) STL map中的count用法 利用dfs输出同一个圈内的名字 题意 题目中给出 n 的人的名字, ...
- APT 信息收集——shodan.io ,fofa.so、 MX 及 邮件。mx记录查询。censys.io查询子域名。
信息收集 目标是某特殊机构,外网结构简单,防护严密.经探测发现其多个子机构由一家网站建设公司建设. 对子域名进行挖掘,确定目标ip分布范围及主要出口ip. 很多网站主站的访问量会比较大.往往主站都是挂 ...
- Calendar.getInstance()获取指定时间点(定时)
public class Test1 { public static void main(String[]args){ System.out.println("时间为:\n"+ge ...
- NOI2019游记 —— 夏花般绚烂,繁星般璀璨
NOI 2019 游记 夏花般绚烂,繁星般璀璨 打算写成两个形式 Dairy Day -1 早早就到gzez集训了20几天,对整体的环境熟悉很多 在gzez看了场LNR Day 2 然后回到宾馆搞了个 ...
- 项目Beta冲刺——凡事预则立
班级:软件工程1916|W 作业:项目Beta冲刺(团队) 团队名称:Echo 作业目标:规定代码规范,明确冲刺任务与计划 目录 团队博客汇总 讨论组长是否重选的议题和结论 下一阶段需要改进完善的功能 ...
- java项目部署
本文章只为帮助大家学习项目的发布,为基础篇,在此给大家示范在window环境下的项目部署及运维. 以下版本为讲解示例,可自行改至匹配版本. 服务器:window service2008 R2 Stan ...
- 【学习笔记】Kruskal 重构树
1. 例题引入:BZOJ3551 用一道例题引入:BZOJ3551 题目大意:有 \(N\) 座山峰,每座山峰有他的高度 \(h_i\).有些山峰之间有双向道路相连,共 \(M\) 条路径,每条路径有 ...
- 微信H5中禁止分享好友及分享到朋友圈的方法
我们可以直接把以下代码加入到页面中,即可限制住各类分享. <script> function onBridgeReady() { WeixinJSBridge.call('hideOpti ...
- urql 高度可自定义&&多功能的react graphql client
urql 是一个很不错的graphql client,使用简单,功能强大,通过exchanges 实现了完整的自定义特性 通过urql 的exchanges 我们可以实现灵活的cache策略 参考资料 ...
- shell脚本编程基础之case语句
基础简介 脚本编程分为: 面向过程 选择结构:if语句,单分支.双分支.多分支:case语句 控制结构:顺序结构(默认) 循环结构:for.while.until 面向对象 case语句结构 case ...