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. Netty4.x中文教程系列(五)编解码器Codec

    Netty4.x中文教程系列(五)编解码器Codec 上一篇文章详细解释了ChannelHandler的相关构架设计,版本和设计逻辑变更等等. 这篇文章主要在于讲述Handler里面的Codec,也就 ...

  2. JSP相对路径与绝对路径探秘

    浏览器端 带杠的 一开始浏览器的地址http://localhost:8080/example/index.jsp 如果写成 <a href="/servlet/TestDBSvl&q ...

  3. USACO Section 3.4: Raucous Rockers

    简单的dfs题目 /* ID: yingzho1 LANG: C++ TASK: rockers */ #include <iostream> #include <fstream&g ...

  4. JavaScript DOM实战:创建和克隆元素

    DOM来创建和克隆元素. createElement()和createTextNode() createElement()和createTextNode()做的事情正如它们的名字所说的那样.最常见的J ...

  5. python2 和python3共存下问题

    一.使用python2 or python3 1. 使用python2 $ python xxx.py 2. 使用python3 $ python3 xxx.py 二.脚本调用 /usr/bin/en ...

  6. Android端通过HttpURLConnection上传文件到服务器

    Android端通过HttpURLConnection上传文件到服务器 一:实现原理 最近在做Android客户端的应用开发,涉及到要把图片上传到后台服务器中,自己选择了做Spring3 MVC HT ...

  7. GitHub的使用(上)—— 创建和更新

    推荐一个属于自己的代码控制工具(或者是叫代码托管工具)——GitHub. 提起代码控制工具,很容易想到的就是CVS,SVN.这也是开发团队常用的.但如果想管理只属于自己的代码呢?那它们就不太合适了—— ...

  8. hihoCoder 1043 完全背包 (dp)

    http://hihocoder.com/problemset/problem/1043 动态转移方程 :for v=cost..V f[v]=max(f[v],f[v-c[i]]+w[i]); #i ...

  9. 【ACM】魔方十一题

    0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...

  10. Toad

    1. Toad 规矩: toad 不会违反, 限制, 扩大 你当前用户的权限, toad 不会影响你定义的关于instance的内容. 2. toad 可以执行大部分在 sql*plus 中执行的命令 ...