Implement Queue using Stacks(用栈实现队列)
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 top
,size
,
andis 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).
- 题目解析:
- 用堆栈stack实现队列,仅仅能使用堆栈的操作,push(),pop()。empty().
- 方法:
- 用两个堆栈实现。代码例如以下:
class Queue {
public:
stack<int> st1;
stack<int> st2;
bool st1_use=1;
bool st2_use=0;
// Push element x to the back of queue.
void push(int x) {
if(st1_use==1)
st1.push(x);
else if(st2_use==1)
st2.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(st1.empty()&&st2.empty())
exit(0);
if(st1_use==1&&st2_use==0)
{
while(st1.size()>1)
{
int temp=st1.top();
st2.push(temp);
st1.pop();
}
st1.pop();
while(st2.size()>0)
{
int temp=st2.top();
st1.push(temp);
st2.pop();
}
st1_use==1;
st2_use==0;
return;
}
if(st1_use==0&&st2_use==1)
{
while(st2.size()>1)
{
int temp=st2.top();
st1.push(temp);
st2.pop();
}
st2.pop();
while(st1.size()>0)
{
int temp=st1.top();
st2.push(temp);
st1.pop();
}
st1_use==0;
st2_use==1;
return;
}
}
// Get the front element.
int peek(void) {
if(st1_use==1&&st2_use==0)
{
int temp;
while(st1.size()>0)
{
temp=st1.top();
st2.push(temp);
st1.pop();
}
while(st2.size()>0)
{
int temp1=st2.top();
st1.push(temp1);
st2.pop();
}
st1_use==1;
st2_use==0;
return temp;
}
if(st1_use==0&&st2_use==1)
{
int temp;
while(st2.size()>0)
{
int temp=st2.top();
st1.push(temp);
st2.pop();
}
while(st1.size()>0)
{
int temp1=st1.top();
st2.push(temp1);
st1.pop();
}
st1_use==0;
st2_use==1;
return temp;
}
}
// Return whether the queue is empty.
bool empty(void) {
if(st1_use==1&&st1.empty())
return true;
if(st2_use==1&&st2.empty())
return true;
return false;
}
};
- 方法:
- 用两个堆栈实现。代码例如以下:
class Queue {
public:
stack<int> st1;
stack<int> st2;
bool st1_use=1;
bool st2_use=0;
// Push element x to the back of queue.
void push(int x) {
if(st1_use==1)
st1.push(x);
else if(st2_use==1)
st2.push(x);
} // Removes the element from in front of queue.
void pop(void) {
if(st1.empty()&&st2.empty())
exit(0);
if(st1_use==1&&st2_use==0)
{
while(st1.size()>1)
{
int temp=st1.top();
st2.push(temp);
st1.pop();
}
st1.pop();
while(st2.size()>0)
{
int temp=st2.top();
st1.push(temp);
st2.pop();
}
st1_use==1;
st2_use==0;
return;
}
if(st1_use==0&&st2_use==1)
{
while(st2.size()>1)
{
int temp=st2.top();
st1.push(temp);
st2.pop();
}
st2.pop();
while(st1.size()>0)
{
int temp=st1.top();
st2.push(temp);
st1.pop();
}
st1_use==0;
st2_use==1;
return;
}
} // Get the front element.
int peek(void) {
if(st1_use==1&&st2_use==0)
{
int temp;
while(st1.size()>0)
{
temp=st1.top();
st2.push(temp);
st1.pop();
} while(st2.size()>0)
{
int temp1=st2.top();
st1.push(temp1);
st2.pop();
}
st1_use==1;
st2_use==0;
return temp;
}
if(st1_use==0&&st2_use==1)
{
int temp;
while(st2.size()>0)
{
int temp=st2.top();
st1.push(temp);
st2.pop();
} while(st1.size()>0)
{
int temp1=st1.top();
st2.push(temp1);
st1.pop();
}
st1_use==0;
st2_use==1;
return temp;
}
} // Return whether the queue is empty.
bool empty(void) {
if(st1_use==1&&st1.empty())
return true;
if(st2_use==1&&st2.empty())
return true;
return false;
} };
Implement Queue using Stacks(用栈实现队列)的更多相关文章
- LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
- [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 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 232 Implement Queue using Stacks 用栈来实现队列
使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- 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 ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
随机推荐
- Scrum Meeting Alpha - 8
Scrum Meeting Alpha - 8 NewTeam 2017/11/2 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了登录API的测试和包装Pull Requ ...
- ASP.NET Core 依赖注入(DI)简介
ASP.NET Core是从根本上设计来支持和利用依赖注入. ASP.NET Core应用程序可以通过将其注入到Startup类中的方法中来利用内置的框架服务,并且应用程序服务也可以配置为注入. AS ...
- Getting Started With setuptools and setup.py
https://pythonhosted.org/an_example_pypi_project/setuptools.html http://www.ianbicking.org/docs/setu ...
- ButterKnife的使用以及不能自动生成代码问题的解决
ButterKnife的使用以及不能自动生成代码问题的解决 转载请注明出处:http://www.cnblogs.com/zhengjunfei/p/5910497.html 最近换了个工作刚入职,又 ...
- ssh简单配置
Port 2223Protocol 2HostKey /etc/ssh/ssh_host_rsa_keyHostKey /etc/ssh/ssh_host_dsa_keyKeyRegeneration ...
- Maven启动Java Web工程,8081和8086端口号被占用
Maven启动Java Web工程, <!-- 配置tomcat插件 --> <build> <plugins> <plugin> <groupI ...
- vim操作命令
一,命令模式下 文件顶部: gg 文件底部: G 删除当前行:dd 删除当前行,并进入INSERT模式: cc 取消删除:u
- windows下安装和redis主从配置(通过哨兵控制主从切换)
首先自己先得了解什么是redis,这里就不详做介绍什么是redis了,这篇文章主要讲的是怎么样配置 redis怎样配置主从关系和哨兵控制主从服务器的配置以及应用,就当是给自己记笔记吧! 1.下载red ...
- work 2013-07-19
今天,在现场进行了数据库的优化,将数据库的日志截断和压缩了 use 测试库backup log 测试库 with no_logdbcc shrinkfile (测试库_Data,1)dbcc shri ...
- C语言之最大公约数与最小公倍数
#include<stdio.h>int main(){ int num1, num2,temp; scanf("%d%d",&num1,&num2); ...