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. hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据

    参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...

  2. 解决COS、FileUpload上传文件时中文文件名乱码问题

    方法: MultipartParser mp = new MultipartParser(request, 10*1024*1024); mp.setEncoding("GBK") ...

  3. Control character in cookie value, consider BASE64 encoding your value

    这是因为你给Cookie设置了中文的value,比如Cookie c = new Cookie("user", "张三");

  4. Elsevier 投稿各种状态总结

    Elsevier 投稿各种状态总结1. Submitted to Journal      当上传结束后,显示的状态是Submitted to Journal,这个状态是自然形成的无需处理.2. Wi ...

  5. ajax返回JSON时的处理方式

    JSON中对象通过“{}”来标识,一个“{}”代表一个对象,如{“AreaId”:”123”},对象的值是键值对的形式(key:value). json_encode() 该函数主要用来将数组和对象, ...

  6. Activity 跳转动画 全局定义

    定义application 的 theme <application android:allowBackup="true" android:icon="@drawa ...

  7. Git查看、删除、重命名远程分支和tag【转】

    转自:http://zengrong.net/post/1746.htm 本站文章除注明转载外,均为本站原创或者翻译. 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也 ...

  8. IO(一)

    文件相关 package com.bjsxt.io.file; import java.io.File; /** * 两个常量 * 1.路径分隔符 ; * 2.名称分隔符 /(windows) /(l ...

  9. [androd] android的在线源码网站,各个版本都有(目前已到俺android 4.2,但不包含kernel部分的代码)

    android的在线源码阅读网站,各个版本都有(目前最新版本已到android 4.2,但不包含kernel部分的代码) 这个网站最大的特点是:可以在网页上方的搜索框,搜索整个网站所存储的源码中的字符 ...

  10. hiho #1332 : 简单计算器 栈+递归

    #1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...