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 toppeek/pop from topsize, and is empty operations 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 用栈来实现队列的更多相关文章

  1. 232 Implement Queue using Stacks 用栈来实现队列

    使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...

  2. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  3. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. (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 ...

  6. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  7. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

  8. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  9. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

随机推荐

  1. charAt,charCode,fromCharCode区别

    1.charAt 返回字符串指定位置的字符 2.charCode 返回字符串指定位置字符Unicode编码 3.fromCharCode 用Unicode编码创建字符串 我们来看下例子 var str ...

  2. MSc in Robotics

    MSc in RoboticsProgramming Methods for Robotics AssignmentIrene Moulitsas & Peter SherarCranfiel ...

  3. tensorflow API _ 5 (tensorflow.summary)

    tensorflow的可视化是使用summary和tensorboard合作完成的. 基本用法 首先明确一点,summary也是op. 输出网络结构 with tf.Session() as sess ...

  4. 域渗透:SPN(ServicePrincipal Names)的利用

    SPN 简介:服务主体名称(SPN:ServicePrincipal Names)是服务实例(可以理解为一个服务,比如 HTTP.MSSQL)的唯一标识符.Kerberos 身份验证使用 SPN 将服 ...

  5. BZOJ 2200: [Usaco2011 Jan]道路和航线

    Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...

  6. 抓取Dump文件的方法和工具介绍

    一.Windows系统的任务管理器里抓dump 启动任务管理器,选中某个进程,右键,弹出菜单"创建转储文件" 注意事项: 当你在64位Windows系统上抓32位进程的dmup文件 ...

  7. Vue中插槽slot的使用

    插槽,也就是slot,是组件的一块HTML模板,这块模板显示不显示.以及怎样显示由父组件来决定. 实际上,一个slot最核心的两个问题在这里就点出来了,是显示不显示和怎样显示. 由于插槽是一块模板,所 ...

  8. 洛谷P4343 [SHOI2015]自动刷题机

    题目 易得该题目中的\(n\)和\(k\)具有单调性,满足二分的性质,因此该题目而已用二分来枚举\(n\),然后对于每个\(n\)模拟出它所对应的\(k\),然后注意注意代码细节,并且当当前\(k\) ...

  9. Python各种扩展名(py, pyc, pyw, pyo, pyd)区别

    扩展名 在写Python程序时我们常见的扩展名是py, pyc,其实还有其他几种扩展名.下面是几种扩展名的用法. py py就是最基本的源码扩展名 pyw pyw是另一种源码扩展名,跟py唯一的区别是 ...

  10. Connection to newtaotao failed. [08001] Could not create connection to database

    jdbc.url=jdbc:mysql://localhost:3306/newtaotao?serverTimezone=UTC&characterEncoding=utf-8 数据库是5. ...