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 topsize, and is 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).
 class Queue {
stack<int> s1;
stack<int> s2;
public:
// Push element x to the back of queue.
void push(int x) {
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s1.push(x);
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
} // Removes the element from in front of queue.
void pop(void) {
s1.pop();
} // Get the front element.
int peek(void) {
return s1.top();
} // Return whether the queue is empty.
bool empty(void) {
return s1.empty();
}
};

Implement Queue using Stacks的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  4. 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 ...

  5. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

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

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

  7. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

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

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

  9. Leetcode Implement Queue using Stacks

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

随机推荐

  1. Swift中的部分更新与旧版的区别

    1. 函数中的外部变量名取消 “#”方式,仅能用直接命名方式 错误 func swift(#str :NSString){} 正确 func swift(str str :NSString){} 2. ...

  2. 生成uuid

    function guid(){ if (function_exists('com_create_guid')){ return com_create_guid(); }else{ mt_srand( ...

  3. redmine + apache + mod_fcgid

    redmine默认是用webrick启动的,这种方法不适合生产环境,最好部署在apache下,本文介绍如何通过mod_fcgid启动. 首先要有一个能够启动的redmine,可以通过我之前的博文查看: ...

  4. 6、软件配置工程师要阅读的书籍 - IT软件人员书籍系列文章

    软件配置管理工程师的工作也是贯穿整个项目过程的.其主要针对项目中的各种文档.技术源码等等进行归档控制.一般的配置项比如需求说明书,概要设计,详细设计,测试文档,用户手册等,还有源代码管理,数据库文档文 ...

  5. Asp.net MVC Razor模板引擎技巧分享

    Razor是Asp.net MVC中新的默认模板类型, 语法简单易用.这篇文章不涉及Razor的语法,主要介绍Razor的一些在MVC项目中的使用技巧,以及脱离MVC环境下,如何使用Razor. 阅读 ...

  6. JavaScript Patterns 5.1 Namespace Pattern

    global namespace object // global object var MYAPP = {}; // constructors MYAPP.Parent = function() { ...

  7. vs中不得不会的一些小技巧(1)——细说查找

    最近在改公司里面古老的asp代码,不说文件有1w个,起码也有7,8千,而且文件里面include一个嵌套一个...当某天jira平台 上出现了需要你改的bug的时候,甚至都不知道这个错误在哪个页面,更 ...

  8. MySQL数据库备份命令

    原文参考:MySQL数据库备份的命令 - 司南 mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql备 ...

  9. Js 关于console 在IE 下的兼容问题

    程序员在开发代码的过程中,使用console作为调试代码过程的一种手段. 发布到测试生产环境,发现IE8 出现加载错误.使用开发者工具调试,发现可以绕过问题. 通过网络搜索和在项目中进行修正. 以下办 ...

  10. Tomcat免安装配置

    大家都知道tomcat吧!因为Tomcat 技术先进.性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器,也是运行Servlet和JS ...