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 ...
随机推荐
- jquery getScript动态加载JS方法改进详解[转载]
转载自http://www.jb51.net/article/31973.htm 有许多朋友需要使用getScript方法动态加载JS,本文将详细介绍此功能的实现方法 $.getScript( ...
- JAVASE高级2
反射概述 什么是反射? 反射的的概念是有smith1982年首次提出的,zhuy主要是指程序可以访问.检测和修改它本身状态或行为的一种能力. JAVA反射机制是运行状态中,对于任意一个类,都能够知道这 ...
- ⑨bootstrap组件 按钮式下拉菜单 输入框 使用基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- Python 3.X 调用多线程C模块,并在C模块中回调python函数的示例
由于最近在做一个C++面向Python的API封装项目,因此需要用到C扩展Python的相关知识.在此进行简要的总结. 此篇示例分为三部分.第一部分展示了如何用C在Windows中进行多线程编程:第二 ...
- C# 程序集安装与卸载
下面我们来实现程序集的安装,代码如下: /// <summary>/// 程序集安装与卸载/// </summary>/// <param name="asse ...
- [转载] java多线程学习-java.util.concurrent详解(四) BlockingQueue
转载自http://janeky.iteye.com/blog/770671 ------------------------------------------------------------- ...
- 基于Spark和SparkSQL的NetFlow流量的初步分析——scala语言
基于Spark和SparkSQL的NetFlow流量的初步分析--scala语言 标签: NetFlow Spark SparkSQL 本文主要是介绍如何使用Spark做一些简单的NetFlow数据的 ...
- ASP.NET Core教程【一】关于Razor Page的知识
关键文件和目录结构 按照asp.net core WEB应用程序向导,创建一个工程之后 你会发现如下几个目录和文件 wwwroot:放置网站的静态文件的目录 Pages:放置razor页面的目录 ap ...
- 移动端页面input输入框被键盘遮挡问题
<body class="layout-fixed"> <!-- fixed定位的头部 --> <header> </header> ...
- 七、Hadoop学习笔记————调优之Hadoop参数调优
dfs.datanode.handler.count默认为3,大集群可以调整为10 传统MapReduce和yarn对比 如果服务器物理内存128G,则容器内存建议为100比较合理 配置总量时考虑系统 ...