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 ...
随机推荐
- 跨域 Ajax 其他可选技术 异步
使用image pings 最早的跨域方法之一就是使用这个,任何域的<img>和<script>元素都可以随便加载. var img = new Image(); img.on ...
- ⑨bootstrap组件 按钮式下拉菜单 输入框 使用基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- 谈一次java web系统的重构思路
——略谈Java web软件如何提供二次开发接口 接手公司的一个Java web软件产品,该软件采用传统的dwr框架.dwr框架相当于一个中间层,使得javascript能够识别Java类对象,进而能 ...
- jquery IE6 下animate 动画的opacity无效
jquery IE6 下animate 动画的opacity无效,其实是有效的,因为IETester的一个小BUG 原生IE6 没问题...呵呵~~
- CentOS6.8系统下,ecipse下进行编辑操作,意外退出
错误情况:centos下打开eclipse软件,点击*.java或者*.pom软件卡死,命令行终端报错误信息,稍后eclipse自动退出. 错误信息: Java: cairo-misc.c:380: ...
- 高阶函数实现AOP
AOP(面向切面程序)的主要作用是把一些跟核心业务逻辑模块无关的功能抽离出来,这些跟业务逻辑无关的功能通常包括日至统计.安全控制.异常处理等.把这些功能抽离出来之后,再通过"动态织入&quo ...
- 最近ssh遇到异常及解决
1.SSH框架,spring和struts整合,action中注入service不成功,检测是否缺少 struts2-spring-plugin-2.3.4.1.jar包 2.字符串转 json 加了 ...
- JAVA构造函数简析
构造函数是java新建对象的一种手段 构造函数可以重载 如果一个类中有多个域,那么就可能需要多个构造函数.这时候,使用重载就可以了 构造函数中this和super的使用 this:(1)this用于本 ...
- Intrumentation类:ActivityInstrumentationTestCase2学习(1)
public abstract class ActivityInstrumentationTestCase2 extends ActivityTestCase//继承自ActivityTestCase ...
- 预加载(图片,css ,js)
图片预加载 new Image().src = 'http://img1.t.sinajs.cn/t35/skin/skin_008/skin.css'; //新浪(4) 非ie下预加载(js,css ...