比较典型的一个题目,easy,不过可以有许多实现方式。

这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中。但是其他的实现也可以不是这样,可以是需要push的时候检查再,如果内容在stack2中,这时候将其倒回在进行push。这里采取第一种比较笨的方法,代码如下所示:

 class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
s1.push(x);
} // Removes the element from in front of queue.
void pop(void) {
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s2.pop();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
} // Get the front element.
int peek(void) {
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
int ret = s2.top();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
return ret;
} // Return whether the queue is empty.
bool empty(void) {
return s1.empty();
}
private:
stack<int> s1;
stack<int> s2;
};

LeetCode OJ:Implement Queue using Stacks(栈实现队列)的更多相关文章

  1. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  2. [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  3. LeetCode 232 Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  4. leetCode(37):Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. (easy)LeetCode 232.Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  6. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  7. LeetCode(23)-Implement Queue using Stacks

    题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...

  8. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  9. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

  10. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

随机推荐

  1. sqoop命令,mysql导入到hdfs、hbase、hive

    1.测试MySQL连接 bin/sqoop list-databases --connect jdbc:mysql://192.168.1.187:3306/trade_dev --username ...

  2. ElasticSearch(一)ElasticSearch的应用场景及为什么要选择ElasticSearch?

    先了解一下数据的分类 结构化数据又可以称之为行数据,存储在数据库里,可以用二维表结构来逻辑表达实现的数据.其实就是可以能够用数据或者统一的结构加以表示的数据.比如在数据表存储商品的库存,可以用整型表示 ...

  3. DevOps实践之持续集成Jenkins(最新版本测试)

    一.安装Jenkins (1)下载并安装配置Java Development Kit 8 [root@localhost ~]# rpm -ivh jdk-8u161-linux-x64.rpm Pr ...

  4. 如何用纯 CSS 创作一个跳 8 字型舞的 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/gKNMMm 可交互视频 此视频是可 ...

  5. 20145312 《Java程序设计》第九周学习总结

    20145312 <Java程序设计>第九周学习总结 学习笔记 Chapter 16整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 SUN公司为了简化.统一对数据库的操作, ...

  6. 20145327实验二Java面向对象程序设计

    Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 伪代码,产品代码,测试代码的应用 ...

  7. 20135302魏静静——linux课程第七周实验及总结

    linux课程第七周实验及总结 实验及学习总结 1. 编译链接的过程和ELF可执行文件格式(以hello为例) GNU编译系统编译源码: 首先,运行C预处理器(cpp),将.c文件翻译成.i文件——g ...

  8. Linux文件系统学习(一)之相关概念⭐⭐⭐

    “一切皆是文件”是 Unix/Linux 的基本哲学之一.不仅普通的文件,目录.字符设备.块设备.套接字等在 Unix/Linux 中都是以文件被对待:它们虽然类型不同,但是对其提供的却是同一套操作界 ...

  9. ASP.NET MVC 必备开发环境

    许多初学者为了搭建开发环境,很多软件找不齐,或者找不到的比较新而且稳定版本.所以我将下载和安装的资料整理了下,供大家下载.资料均收集于网络,但基本核实资料的可靠性,但不能完全保证.如果你在使用过程中发 ...

  10. 单调队列:temperature

    题目大意:某国进行了连续n天的温度测量,测量存在误差,测量结果是第i天温度在[l_i,r_i]范围内. 求最长的连续的一段,满足该段内可能温度不降. 第一行n下面n行,每行l_i,r_i 1<= ...