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 emptyoperations 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 ...
随机推荐
- HTML学习笔记 w3sCss盒子模型(阴影)(div的一些使用)案例 第十节 (原创) 参考使用表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Django----中间件详解
Django中间件 在http请求 到达视图函数之前 和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 中间件的执行流程 1.执行完所有的request ...
- 29.使用register_chrdev_region()系列来注册字符设备
1.之前注册字符设备用的如下函数注册字符设备驱动: register_chrdev(unsigned int major, const char *name,const struct file_ope ...
- WPF 简易的喷泉效果
这两天领导让我做个喷泉的效果,要把一个个UserControl从一个位置喷出,然后,最后落在最终需要在的位置. 喷泉效果说白了,就是两个步骤:1.放大,从0放大到需要的倍数:2.缩小,平移,从放大的倍 ...
- 前后端分手大师——MVVM 模式
之前对 MVVM 模式一直只是模模糊糊的认识,正所谓没有实践就没有发言权,通过这两年对 Vue 框架的深入学习和项目实践,终于可以装B了有了拨开云雾见月明的感觉. 简而言之 Model–View–Vi ...
- Nosql简介 Redis,Memchche,MongoDb的区别
本篇文章主要介绍Nosql的一些东西,以及Nosql中比较火的三个数据库Redis.Memchache.MongoDb和他们之间的区别.以下是本文章的阅读目录 一.Nosql介绍 1.Nosql简介 ...
- Android 在通知栏实现计时功能
Notification是APP 向系统发出通知时,它将先以图标的形式显示在通知栏中.用户可以下拉通知栏查看通知的详细信息.我们可以在通知栏实现自定义的效果,也可以结合service和BroadCas ...
- (ajax)——jquery用法
例子:/* ajax获得状态 */ 点击事件 $("#findBycname").click(function(){ var company = ...
- Activity讲解
Activity Activity 是 Android 应用的重要组成单元之一(另外三个是 Service.BroadcastReceiver 和 ContentProvider),而 Activit ...
- 使用sklearn进行数据挖掘-房价预测(1)
使用sklearn进行数据挖掘系列文章: 1.使用sklearn进行数据挖掘-房价预测(1) 2.使用sklearn进行数据挖掘-房价预测(2)-划分测试集 3.使用sklearn进行数据挖掘-房价预 ...