leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一、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).
分析:用(两个)队列实现栈
设置两个队列分别为1和2
(1)入栈:如果队列2当前有元素,而队列1为空(反之亦然),那么将需要入栈的元素放入队列1中,然后将队列2中的元素依次出队并入队到队列1中。(即要保证有一个队列是空的)
(2)出栈:将有元素(不为空)的队列出队即可-------如:
先将元素a插入队列1中 ,现在要将元素b入栈,则将b插入到队列2中然后将队列1中的a出队到队列2中,则队列2中的元素变为 b,a
这样队列1为空,现在要压入c, 则将c插入队列1中 ,依次将队列2中的b ,a出队并加入到队列1中 ,则队列1中的元素变为 c,b,a,而队列2为空
(保证一队列为空)
代码如下:
【两个队列】
class Stack {
public:
// Push element x onto stack.
queue<int> queue1;
queue<int> queue2;
void push(int x) {
if (queue1.empty())
{
queue1.push(x);
while(!queue2.empty()){
int tmp = queue2.front();
queue2.pop();
queue1.push(tmp);
}
}else{
queue2.push(x);
while(!queue1.empty()){
int tmp = queue1.front();
queue1.pop();
queue2.push(tmp);
}
}
}
// Removes the element on top of the stack.
void pop() {
if (!queue1.empty())
queue1.pop();
if (!queue2.empty())
queue2.pop();
}
// Get the top element.
int top() {
if (!queue1.empty())
return queue1.front();
if (!queue2.empty())
return queue2.front();
}
// Return whether the stack is empty.
bool empty() {
return queue1.empty() && queue2.empty();
}
};
其他解法:
【两个队列】用两个队列myStack,temp实现一个栈。push时把新元素添加到myStack的队尾。pop时把myStack中除最后一个元素外逐个添加到myStack中,然后pop掉myStack中的最后一个元素,然后注意记得myStack和temp,以保证我们添加元素时始终向temp中添加。
class Stack {
public:
// Push element x onto stack.
void push(int x) {
myStack.push(x);
}
// Removes the element on top of the stack.
void pop() {
std::queue<int> temp;
int len = myStack.size();
for(int i = 0; i < len - 1; i++) {
temp.push(myStack.front());
myStack.pop();
}
myStack = temp;
}
// Get the top element.
int top() {
if(myStack.size() != 0) return myStack.back();
}
// Return whether the stack is empty.
bool empty() {
if(myStack.size() == 0) return true;
else return false;
}
private:
std::queue<int> myStack;
};
或:
【两个队列】
class Stack {
queue<int> rev_q;
public:
// Push element x onto stack.
void push(int x) {
queue<int> temp_q;
temp_q.push(x);
while (!rev_q.empty()) {
temp_q.push(rev_q.front());
rev_q.pop();
}
rev_q = temp_q;
}
// Removes the element on top of the stack.
void pop() {
rev_q.pop();
}
// Get the top element.
int top() {
return rev_q.front();
}
// Return whether the stack is empty.
bool empty() {
return rev_q.empty();
}
};
【一个队列】---push时直接添加到队尾就好。pop和top时,把队列除最后一个元素外,逐个循环添加到队列的尾部。
class Stack {
public:
// Push element x onto stack.
void push(int x) {
unsigned int size = s.size();
this->s.push(x);
while (size--){
s.push(s.front());
s.pop();
}
}
// Removes the element on top of the stack.
void pop() {
s.pop();
}
// Get the top element.
int top() {
return s.front();
}
// Return whether the stack is empty.
bool empty() {
return s.empty();
}
private:
queue<int> s;
};
附注:队列queue的成员函数
- empty()判断队列空,当队列空时,返回true。
- size()访问队列中的元素个数。
- push()会将一个元素置入queue中。
- front()会返回queue内的第一个元素(也就是第一个被置入的元素)。
- back()会返回queue中最后一个元素(也就是最后被插入的元素)。
- pop()会从queue中移除一个元素。[1]
- 注意:pop()虽然会移除下一个元素,但是并不返回它,front()和back()返回下一个元素但并不移除该元素。
二、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:
stack<int> stack1;
stack<int> stack2;
// Push element x to the back of queue.
void push(int x) {
stack1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(!stack2.empty()) stack2.pop();
else{
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
stack2.pop();
}
}
// Get the front element.
int peek(void) {
if(!stack2.empty()) return stack2.top();
else{
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
return stack2.top();
}
}
// Return whether the queue is empty.
bool empty(void) {
return stack1.empty() && stack2.empty();
}
};
注:stack2.push(stack1.top())中若写成 stack2.push(stack1.pop())则会出错:invalid use of void expression
或:
class Queue {
public:
stack<int> s1;
stack<int> s2;
// Push element x to the back of queue.
void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(s1.empty())
return;
while(!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
s2.pop();
while(!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
}
// Get the front element.
int peek(void) {
if(s1.empty())
return -1;
while(!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
int t = s2.top();
while(!s2.empty()) {
s1.push(s2.top());
s2.pop();
}
return t;
}
// Return whether the queue is empty.
bool empty(void) {
return s1.empty();
}
};
可参考其他解法:
我们做过一道相反的题目Implement Stack using Queues 用队列来实现栈。这道题颠倒了个顺序,起始并没有太大的区别,栈和队列的核心不同点就是栈是先进后出,而队列是先进先出,那么怎么用栈的先进后出的特性来表示出队列的先进先出呢?方法是:只要在插入元素的时候每次都从前面插入即可,即如果一个队列是1,2,3,4,那么就在栈中保存为4,3,2,1,那么返回栈顶元素1,即为队列的首元素。我们可以设置一个辅助栈tmp,把s的元素也逆着顺序存入tmp中,此时若加入新元素x,再把tmp中的元素倒回来。
代码如下:
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
stack<int> tmp;
while (!s.empty()) {
tmp.push(s.top());
s.pop();
}
s.push(x);
while (!tmp.empty()) {
s.push(tmp.top());
tmp.pop();
}
}
// Removes the element from in front of queue.
void pop(void) {
s.pop();
}
// Get the front element.
int peek(void) {
return s.top();
}
// Return whether the queue is empty.
bool empty(void) {
return s.empty();
}
private:
stack<int> s;
};
leetcode:Implement Stack using Queues 与 Implement Queue using Stacks的更多相关文章
- 225. Implement Stack using Queues + 232. Implement Queue using Stacks
▶ 栈和队列的相互表示.发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小. ▶ 第 225 题,用队列实现栈 ● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出 ...
- 【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( ...
- 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- Spring Mvc 返回机制
转自:http://jianzh5.iteye.com/blog/1909234 Spring Mvc 有如下的几种返回方式: ModelAndView, Model, ModelMap, Map, ...
- 学习NAnt Build .CS+Solution+MSBuild+SVN+NUnit+NUnitReport
NAnt help:http://nant.sourceforge.net/release/latest/help/tasks/NAntContrib help:http://nantcontrib. ...
- 1024: [SCOI2009]生日快乐
暴力题,N<=10,没注意到平均分,读题真是.. 我们对于一个矩形分成两块进行搜.然后求较大值. ans=min(ans,max(dfs(x,y/n*i,i),dfs(x,y/n*(n-i),n ...
- org.apache.kafka.common.network.Selector
org.apache.kafka.common.client.Selector实现了Selectable接口,用于提供符合Kafka网络通讯特点的异步的.非阻塞的.面向多个连接的网络I/O. 这些网络 ...
- 正确使用HTML title属性
如果你想对使用手机,平板电脑和辅助技术的用户隐藏某些内容,而只对键盘用户显示,那么请使用title属性. 细节 HTML的title属性本身有问题.之所以有问题是因为它在一些重要的方面表现的不够好,尽 ...
- F.I.S本地环境的搭建教程
一.准备开发环境: 1.安装JRE 2.安装nodejs 最好是msi文件,比较省事. 3.(如果是PHP项目)安装php. 首先下载php(我的是5.5.15版本,win7 64位系统) zip,然 ...
- POJ 1845 Sumdiv (求某个数的所有正因子的和)
题意: 求A^B的所有正因子的和,最后模9901的结果. 思路: 若对一个数n进行素数分解,n=p1^a1*p2^a2*p3^a3*...*pk^ak那么n的所有正因子之和sum=(1+p1+...+ ...
- 01 - 开发成功的Oracle应用
笔记 1. 开发数据库应用,不能把数据库当黑盒.需要了解数据库的一下内容 数据库的体系结构 并发控制 开发的时候就要调优你的代码 数据库有哪些特性,不要在你的代码里重复实现 深入的学习SQL 2. 我 ...
- spring_150804_controller
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- 安卓RadioButton的使用
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...