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. vue 中 axios 使用

    前言 在对接接口的时候时常会有传参问题调调试试很多,是 JSON.From Data还是 URL 传参,没有搞清楚就浪费很多时间. 本文中就结合 axios 来说明这些的区别,以便在以后工作更好对接. ...

  2. 使用Apache commons-maths3-3.6.1.jar包,在hive上执行获取最大值的操作

    udf是对hive上的每行(单行)数据操作,现在我要实现对hive上的一列数据操作,udf函数已经满足不了我的要求了,使用udaf对hive的一列求最大值: 代码如下: package com; im ...

  3. JS优化常用片断

    防抖debounce装饰器 在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时. function debounce(func, delay) { let isCooldown = fa ...

  4. BZOJ 5082: 弗拉格 矩阵乘法

    如果单点而不是求 sigma 的话还是比较好办的. 遇到这种前缀和相减的矩阵乘法可以增设一个 0 使得后面的能先加到前面,然后再算. 这样的话可以使的最后算出的是前缀和相加的形式. code: #in ...

  5. hasura graphql-engine 最近版本的一些更新

    好久没有在关注hasura graphql-engine 了,从最新的release 信息可以看到graphql-engine 已经beta 阶段了,而且目前是v1.0.0-beta.2 估计离生产可 ...

  6. Java如何正确的将数值转化为ArrayList?

    Java中使用工具类Arrays.asList()看似可以把一个数组转为List,但实际使用时有两个坑:1.它是泛型方法,传入的参数必须是对象数组,当传入一个原生数据类型数组时,Arrays.asLi ...

  7. SQL基础-汇总统计及GROUP BY

    一.汇总统计 1.聚集函数 COUNT() 计算总数 SUM() 求和 MAX() 最大值 MIN() 最小值 AVG() 平均值 2.聚集函数使用 总共有多少名学生? SELECT COUNT(*) ...

  8. IOI2019题解

    由于太懒了,好久没更新了.发个题解好了. shoes 首先不难证明鞋子配对一定是从前往后将同一种的左和右配对. 配好对之后首先我们可以假设左在右的左边,然后讨论可知将左边靠前的排在前面更优. rect ...

  9. 一次修复linux的efi引导的集中方法总结记录

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/grub_uefi_repair 起因:EFI分区被删除导致引导问 ...

  10. JAVA中Stringbuffer的append( )方法

    Stringbuffer是动态字符串数组,append( )是往动态字符串数组添加,跟“xxxx”+“yyyy”相当‘+’号. 跟String不同的是Stringbuffer是放一起的,String1 ...