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).

代码如下:

class MyQueue {
// Push element x to the back of queue.
Stack<Integer> stack1=new Stack<Integer>();
Stack<Integer> stack2=new Stack<Integer>();
public void push(int x) {
stack1.push(x);
} // Removes the element from in front of queue.
public void pop() {
while(stack1.size()>1) stack2.push(stack1.pop());
stack1.pop();
while(stack2.size()>0) stack1.push(stack2.pop());
} // Get the front element.
public int peek() {
while(stack1.size()>1) stack2.push(stack1.pop());
int x= stack1.peek();
while(stack2.size()>0) stack1.push(stack2.pop());
return x;
} // Return whether the queue is empty.
public boolean empty() {
return stack1.empty();
}
}

  运行结果:

(easy)LeetCode 232.Implement Queue using Stacks的更多相关文章

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

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

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

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

  3. Java [Leetcode 232]Implement Queue using Stacks

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

  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. Java for LeetCode 232 Implement Queue using Stacks

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

  6. Leetcode 232 Implement Queue using Stacks STL

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

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

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

  8. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  9. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

随机推荐

  1. 通过命令行安装Android app

    手动安装安卓app的命令为:adb install -r C:\Users\Lihao\workspace\Appium_Demo\apps\app_F_1.3.0.apk

  2. gcc、g++

    http://hi.baidu.com/zhangcunli8499/item/257e187360b48b2bd6a89cc6 g++ src/*.cpp -I include/ -I includ ...

  3. The error occurred while setting parameters 错误解析--Bad value for type timestamp : 3

    错误信息:nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying datab ...

  4. RMAN备份与恢复之删除过期备份

    使用crosscheck backupset或crosscheck backup之后,提示所有备份集都为available状态,当他执行delete obsolete时,提示有两个文件需要删除.实际上 ...

  5. [shiro] Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.

    访问某页面时,出现了这个异常: java.lang.IllegalArgumentException: Wildcard string cannot be null or empty. Make su ...

  6. window.open窗口关闭后刷新父窗口代码

    window.open窗口关闭后刷新父窗口代码 window.opener.location.href=window.opener.location.href;window.close();

  7. OpenCV图像处理篇之边缘检测算子

    OpenCV图像处理篇之边缘检测算子 转载: http://xiahouzuoxin.github.io/notes/ 3种边缘检测算子 一阶导数的梯度算子 高斯拉普拉斯算子 Canny算子 Open ...

  8. This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms

    异常消息:This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms ...

  9. python学习-day02

    ---恢复内容开始--- 一.pycharm安装 1.1破解方式http://www.cnblogs.com/evlon/p/4934705.html 1.2.头部配置: 二.运算符 2.1.比较运算 ...

  10. 【sql】之使用sql根据身份证查询过生日人数

    根据当前日期查询有多少人过生日 ,) = DATE_FORMAT(NOW(),'%m'); 查询price一样的人数 select * from people where price in (sele ...