232 Implement Queue using Stacks 用栈来实现队列
使用栈来实现队列的如下操作:
push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
注意:
你只能使用标准的栈操作-- 也就是只有push to top, peek/pop from top, size, 和 is empty 操作是可使用的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque (双端队列)来模拟一个栈,只要你仅使用栈的标准操作就可以。
假设所有操作都是有效的,比如 pop 或者 peek 操作不会作用于一个空队列上。
详见:https://leetcode.com/problems/implement-queue-using-stacks/description/
Java实现:
方法一:
class MyQueue { /** Initialize your data structure here. */
private Stack<Integer> stk;
public MyQueue() {
stk=new Stack<Integer>();
} /** Push element x to the back of queue. */
public void push(int x) {
stk.add(0,x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
return stk.pop();
} /** Get the front element. */
public int peek() {
return stk.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
return stk.isEmpty();
}
} /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
方法二:
class MyQueue { /** Initialize your data structure here. */
private Stack<Integer> stkPush;
private Stack<Integer> stkPop;
public MyQueue() {
stkPush=new Stack<Integer>();
stkPop=new Stack<Integer>();
} /** Push element x to the back of queue. */
public void push(int x) {
stkPush.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stkPop.isEmpty()){
while(!stkPush.isEmpty()){
stkPop.push(stkPush.pop());
}
}
return stkPop.pop();
} /** Get the front element. */
public int peek() {
if(stkPop.isEmpty()){
while(!stkPush.isEmpty()){
stkPop.push(stkPush.pop());
}
}
return stkPop.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
return stkPush.isEmpty()&&stkPop.isEmpty();
}
} /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
C++实现:
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() { } /** Push element x to the back of queue. */
void push(int x) {
stkPush.push(x);
} /** Removes the element from in front of queue and returns that element. */
int pop() {
if(stkPop.empty())
{
while(!stkPush.empty())
{
stkPop.push(stkPush.top());
stkPush.pop();
}
}
int val=stkPop.top();
stkPop.pop();
return val;
} /** Get the front element. */
int peek() {
if(stkPop.empty())
{
while(!stkPush.empty())
{
stkPop.push(stkPush.top());
stkPush.pop();
}
}
return stkPop.top();
} /** Returns whether the queue is empty. */
bool empty() {
return stkPush.empty()&&stkPop.empty();
}
private:
stack<int> stkPush;
stack<int> stkPop;
}; /**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/
232 Implement Queue using Stacks 用栈来实现队列的更多相关文章
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- [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 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- LeetCode 232 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 bac ...
- 【一天一道LeetCode】#232. Implement Queue using Stacks
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
随机推荐
- Charm Bracelet-POJ3624(01背包)
http://poj.org/problem?id=3624 Charm Bracelet Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- School Marks-CodeForces
B. School Marks time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- JDBC基础教程:tutorialspoint-jdbc
来自turorialspoint的JDBC基础教程(英文),官网:https://www.tutorialspoint.com/jdbc/index.htm 这个教程在国内已经被翻译成中文(不过是属于 ...
- 解决confluence的乱码问题
使用confluence时发现一些含有中文的页面中,中文都变成了问号. 继续搜索解决方案,发现时数据库中数据的格式不对, 在mysql中输入以下命令: mysql> show variabl ...
- pycharm支持react
安装nodejs插件 使能node 出现下面的变化,在scope里可以定义使用的范围 创建react项目 使能eslint规则检查功能 配置前端启动脚本: https://www.jetbrains. ...
- Linux学习系列之lvs+keepalived
LVS简介 LVS介绍 LVS是Linux Virtual Server的缩写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统,属于4层负载均衡 ipvs和ipvsadm的关系 我们使用配置LV ...
- C#.NET 如何快速输入一个对象事件对应的方法
直接在Textbox图像对象中找到这个对象的KeyPress方法,然后输入触发的事件名称.效率更高,不容易出错. "void TypeAreaKeyPress(object sender, ...
- node-load module
javscript :脚本建共享全局名称空间(全局污染). node:实现CommonJS(公共)模块标准. Node加载模块,有两种方式: 1.通过名称 除非是核心模块,否则被引用的模块最后都会映射 ...
- Android 代码设置Activity 背景透明
当指定Activity 样式 Them.Dialog 时候 又不同意用XML 设置 Activity 的背景颜色的时候 用代码 this.getWindow().getDecorView().setB ...
- C#.NEt-GDI+中的Pen測试
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...