232 - 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 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 {
public:
// Push element x to the back of queue.
void push(int x) {
stk.push(x);
} // Removes the element from in front of queue.
void pop(void) {
stack<int> temp;
while(stk.size()!=){
int x=stk.top();
stk.pop();
temp.push(x);
}
stk.pop();
while(!temp.empty()){
int x=temp.top();
temp.pop();
stk.push(x);
}
} // Get the front element.
int peek(void) {
stack<int> temp;
while(stk.size()!=){
int x=stk.top();
stk.pop();
temp.push(x);
}
int x=stk.top();
while(!temp.empty()){
int x=temp.top();
temp.pop();
stk.push(x);
}
return x;
} // Return whether the queue is empty.
bool empty(void) {
return stk.empty();
}
private:
stack<int> stk;
}

225 - Implement Stack using Queues

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
class Stack {
public:
// Push element x onto stack.
void push(int x) {
q.push(x);
} // Removes the element on top of the stack.
void pop() {
queue<int> temp;
while(q.size()!=){
int x=q.front();
q.pop();
temp.push(x);
}
q=temp;
} // Get the top element.
int top() {
queue<int> temp;
while(q.size()!=){
int x=q.front();
q.pop();
temp.push(x);
}
int x=q.front();
q=temp;
q.push(x);
return x;
} // Return whether the stack is empty.
bool empty() {
return q.empty();
}
private:
queue<int> q;
};

【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues的更多相关文章

  1. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

  2. 【LeetCode】232. Implement Queue using Stacks

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

  3. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  4. 【leetcode】622. Design Circular Queue

    题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. 使用 node-odata 轻松创建基于 OData 协议的 RESTful API

    前言 OData, 相信身为.NET程序员应该不为陌生, 对于他的实现, 之前也有童鞋进行过介绍(见:这里1,这里2). 微软的WCF Data Service即采用的该协议来进行通信, ASP.NE ...

  2. spry菜单栏(二)

    自定义选项卡式面板构件 尽管使用属性检查器可以简化对选项卡式面板构件的编辑,但是属性检查器并不支持自定义的样式设置任务.您可以修改选项卡式面板构件的 CSS 规则,并创建根据自己的喜好设置样式的构件. ...

  3. 《c程序设计语言》读书笔记--反转字符串

    #include "stdio.h" #define Num 100 void reverse(char words[]) { int i, j, c, n=0; while(wo ...

  4. VIM移动

    VIM移动   断断续续的使用VIM也一年了,会的始终都是那么几个命令,效率极低 前几个星期把Windows换成了Linux Mint,基本上也稳定了下来 就今晚,我已经下定决心开始新的VIM之旅,顺 ...

  5. Headfirst JSP 01 (概述)

    HTTP 协议 http 是tcp/ip上层协议, 如果你对这些网络协议还不是太熟悉, 下面提供一个非常简单的解释, tcp负责确保从一个网络节点向另一个网络节点发送文件能作为一个完整的文件到达目的地 ...

  6. JavaScript判断浏览器类型及版本

    JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来分辨,另一 ...

  7. BZOJ 3295 动态逆序对

    调了好久.... 转化成三维偏序,cdq处理. 好像比较快? #include<iostream> #include<cstdio> #include<cstring&g ...

  8. iso中AutoLayout和Autoresizing Mask的区别

    •在iOS6之前,关于屏幕旋转的适配和iPhone,iPad屏幕的自动适配,基本都是由Autoresizing Mask来完成的.但是随着大家对iOS App的要求越来越高,以及今后可能出现的多种屏幕 ...

  9. wxWidgets简单的多线程

    #include <wx/wx.h> #include <wx/thread.h> #include <wx/event.h> #include <wx/pr ...

  10. Write operations are not allowed in read-only mode错误

    (转+作者个人理解) 最近在配置 Structs, Spring 和Hibernate整合的问题: 开启OpenSessionInViewFilter来阻止延迟加载的错误的时候抛出了这个异常: org ...