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. django migrate报错:1005 - Can't create table xxx (errno: 150 "Foreign key constraint is incorrectly formed")

    自从mysql升级,以及使用mariaDB以来,很多不曾更新django中model的外键, 今天,按以前的思路写完外键之后, migrate命令报错: 1005 - Can't create tab ...

  2. db2 mysql oracle 邮件 tomcat ssh telnet ftp samba 账号密码

    db2 mysql oracle 邮件 tomcat ssh telnet ftp samba 账号密码 检测

  3. shell部分面试题

    1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...

  4. MySql添加字段命令

    使用ALTER TABLE命令来向一个表添加字段,示例如下: -- 向t_user表添加user_age字段 ) DEFAULT NULL COMMENT '年龄' AFTER user_email; ...

  5. invoke和begininvoke 区别

    一直对invoke和begininvoke的使用和概念比较混乱,这两天看了些资料,对这两个的用法和原理有了些新的认识和理解. 首先说下,invoke和begininvoke的使用有两种情况: 1. c ...

  6. .net core 多sdk 多版本 环境切换

    在讲述.net core多版本之前,我们先理解一下.net core sdk与.net core runtime之前的联系与区别,根据官网的解释我们可以简单地理解为:sdk是在开发过程中进行使用,而r ...

  7. 使用nginx 正向代理暴露k8s service && pod ip 外部直接访问

    有时在我们的实际开发中我们希望直接访问k8s service 暴露的服务,以及pod的ip 解决方法,实际上很多 nodeport ingress port-forword 实际上我们还有一种方法:正 ...

  8. HTML5 离线应用

    一.离线应用cache manifes文件 HTML5中构建了一个离线(无网络状态)应用,只需创建一个cache manifest文件 可以配置需要的缓存的资源,网络无连接应用任然可以使用,本地读取缓 ...

  9. 原生 ajax 请求

    ajax 即 Asynchronous Javascript And XML,AJAX 不是一门的新的语言,而是对现有持术的综合利用.本质是在 HTTP 协议的基础上以异步的方式与服务器进行通信. 异 ...

  10. P1071 潜伏者

    //Pro:NOIP2009 T1 P1071 潜伏者 #include<iostream> #include<cstdio> #include<cstring> ...