【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(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 top,peek/pop from top,size, andis emptyoperations 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 back,peek/pop from front,size, andis emptyoperations 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的更多相关文章
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
- 【LeetCode】232. Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【leetcode】622. Design Circular Queue
题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
随机推荐
- IE内存泄露与无法回收研究小结
一.内存泄露 之前确实看了很多资料,但这位大哥的话可谓画龙点睛,不是奉承他,一下子就打通了我的任督二脉,请看: trarck 写道 IE下的内存泄露原因就是循环引用,IE的垃圾回收器不能很 ...
- hive报错 Another instance of Derby may have already booted the database
刚装好hive后,启动之后showtables;等正常,退出之后再进入,就发现会报错 Caused by: ERROR XSDB6: Another instance ofDerbymay have ...
- 格子布局 js
<!doctype html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- Data Base sqlServer 组合主键
sqlServer 组合主键 创建表时: create table Person ( Name1 ) not null ,Name2 ) not null primary key(Name1,Na ...
- VC操作ADO的基本策略
一.ADO概述 ADO是Microsoft为最新和最强大的数据访问范例 OLE DB 而设计的,是一个便于使用的应用程序层接口.ADO 使您能够编写应用程序以通过 OLE. DB 提供者访问和操作数据 ...
- 51nod1711 平均数
二分答案.check有多少个区间的平均数>xbi=ai-x;将sm离散化.然后logn求出有多少个小于sm[i].类似于求逆序对的思路. 一直WA一个点...所以我就下载数据特判了TAT #in ...
- poj 3694 pku 3694 Network tarjan求割边 lca
题意:给你一个连通图,然后再给你n个询问,每个询问给一个点u,v表示加上u,v之后又多少个桥.一个最容易想到的办法就是先加边找桥,加边找桥,这样可定超时.那么就可以缩点,因为如果一条边不是桥那么无论怎 ...
- Java之网络编程笔记
网络通讯要素: 1.IP地址 IP地址:用于标记一台计算机的身份证. IP地址由网络地址(确定网络)和主机地址(网络中的主机)组成. IP地址分为A类地址.B类地址.C类地址(常用).D类地址.E类地 ...
- BZOJ 3694 最短路
233333想简单了.... 题解参见http://hzwer.com/3710.html #include<iostream> #include<cstdio> #inclu ...
- XmlElement可以避免由XmlSerializer多余生成的代码
public class Program { static void Main(string[] args) { var alarm = new Alarm() { Code = "1588 ...