225. Implement Stack using Queues + 232. Implement Queue using Stacks
▶ 栈和队列的相互表示。发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小。
▶ 第 225 题,用队列实现栈
● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出栈 O(n) 。
class MyStack
{
private:
queue<int> fakeStack;
public:
MyStack()
{
fakeStack = queue<int>();
}
void push(int x)
{
fakeStack.push(x);
}
int pop()
{
if (fakeStack.empty())
return -;
int i, output;
if (fakeStack.size() == )
{
output = fakeStack.front();
fakeStack.pop();
return output;
}
for (i = ;; i++)
{
output = fakeStack.front(), fakeStack.pop();
if (i == fakeStack.size())// 注意这里 fakeStack.size() 已经发生了改变,不用再 -1 了
return output;
else
fakeStack.push(output);
}
}
int top()
{
if (fakeStack.empty())
return -;
if (fakeStack.size() == )
return fakeStack.front();
int i, output;
for (i = ;; i++)
{
output = fakeStack.front(), fakeStack.pop();
fakeStack.push(output);
if (i == fakeStack.size() - )
return output;
}
}
bool empty()
{
return fakeStack.empty();
}
};
● 大佬的代码,3 ms,队列 queue 具有成员函数 back(),可以用于读取队尾元素。单队列实现,入栈 O(1),读取栈顶元素 O(1),出栈 O(n) 。
class MyStack
{
int curr_size;
queue<int> q1, q2;
public:
MyStack()
{
curr_size = ;
}
void push(int x)
{
q1.push(x);
curr_size++;
}
int pop()
{
if (q1.empty())
return -;
while (q1.size() != )
{
q2.push(q1.front());
q1.pop();
}
int temp = q1.front();
q1.pop();
curr_size--;
queue<int> q = q1;
q1 = q2;
q2 = q;
return temp;
}
int top()
{
return q1.back();
}
bool empty()
{
return q1.empty();
}
};
● 作弊,就使用内置栈结构,
class MyStack
{
private:
stack<int> st;
public:
MyStack()
{
st = stack<int>();
}
void push(int x)
{
st.push(x);
}
int pop()
{
int output = st.top();
st.pop();
return output;
}
int top()
{
return st.top();
}
bool empty()
{
return st.empty();
}
};
▶ 第 232 题,用栈实现队列
● 自己的解法,2 ms,双栈实现,分别命名为 fakeQueueIn 和 fakeQueueOut,使用一个标记 store 来记录数据存放于哪个栈里,需要入队的时候把数据倒进 fakeQueueIn 中,需要读取队头或者出队的时候把数据倒入 fakeQueueOut 中,其他情况下不再调整数据的存放位置。
class MyQueue
{
private:
stack<int> fakeQueueIn,fakeQueueOut;
bool store; // true:数据存储在 fakeQueueIn 中
void moveBetweenStack(stack<int>& in, stack<int>& out)
{
int temp;
for (; !in.empty(); out.push(in.top()), in.pop());
}
public:
MyQueue()
{
fakeQueueIn = stack<int>();
fakeQueueOut = stack<int>();
store = true;
}
void push(int x)
{
if (!store)
{
moveBetweenStack(fakeQueueOut, fakeQueueIn);
store = true;
}
fakeQueueIn.push(x);
}
int pop()
{
int output;
if (store)
{
moveBetweenStack(fakeQueueIn, fakeQueueOut);
store = false;
}
output = fakeQueueOut.top();
fakeQueueOut.pop();
return output;
}
int peek()
{
if (store)
{
moveBetweenStack(fakeQueueIn, fakeQueueOut);
store = false;
}
return fakeQueueOut.top();
}
bool empty()
{
return fakeQueueIn.empty() && fakeQueueOut.empty();
}
};
● 最快的解法,2 ms,压根没用 stack,用的 list
class MyQueue
{
public:
list<int> data;
list<int> buffer;
int dataSize;
MyQueue() : data{}, buffer{}, dataSize{ } {}
void push(int x) {
if (dataSize == )
data.push_back(x);
else
buffer.push_back(x);
++dataSize;
}
int pop()
{
--dataSize;
auto res = data.back();
data.pop_back();
if (data.empty())
{
while (!buffer.empty())
{
data.push_back(buffer.back());
buffer.pop_back();
}
}
return res;
}
int peek()
{
return data.back();
}
bool empty()
{
return dataSize == ;
}
};
● 作弊,使用内置的队列结构,3 ms
class MyQueue
{
private:
queue<int> qq;
public:
MyQueue()
{
qq = queue<int>();
}
void push(int x)
{
qq.push(x);
}
int pop()
{
int output = qq.front();
qq.pop();
return output;
}
int peek()
{
return qq.front();
}
bool empty()
{
return qq.empty();
}
};
225. Implement Stack using Queues + 232. Implement Queue using Stacks的更多相关文章
- 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】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( ...
- 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 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 ...
- 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 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 ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- php---------取汉字的第一个字的首字母
开发中用到的方法,取出第一个汉字的首字母: /** * 取汉字的第一个字的首字母 * @param string $str * @return string|null */ function getF ...
- Assert 类
Assert 类 使用 true/false 命题验证单元测试中的条件. 继承层次结构 System.Object Microsoft.VisualStudio.TestTools.Uni ...
- python学习笔记(二)---编辑工具sublimeText3运行python
转载地址:https://blog.csdn.net/Maek_Tyx/article/details/76933897 1. 打开Sublime text 3 安装package controlSu ...
- 3d世界是怎样呈现到屏幕上的
要把一个3d物体呈现在屏幕上,要经过一系列的步骤. 描述3d世界 把3d世界绘制在二维屏幕上 如何描述一个3D世界? 数学家早就给出了3D世界的模型,我们日常最熟悉的3维坐标系就是一个欧几里得空间(线 ...
- CentOS下Docker的安装及国内镜像配置
系统,CentOS 7,最小化安装. 升级包 >$ sudo yum upgrade 安装Docker >$ sudo yum install docker 下面开始配置国内镜像.国外的实 ...
- Visio2010建立ER图并直接导出为SQL语句
Visio2010建立ER图并直接导出为SQL语句 2013年08月20日 ⁄ 综合 ⁄ 共 2581字 ⁄ 字号 小 中 大 ⁄ 评论关闭 建立数据库时我们需要考虑数据之间的关系,为了理清数据之间的 ...
- 360插件化Replugin爬坑之路
前言 继上次爬完了热修复的坑位,中途爬了各种各样的坑.今天我们来说说插件化Replugin的坑位.Replugin刚出的时候我就看过了.第一次看的时候可能心态不好.没看懂= =第二次重头在看,发现蛮简 ...
- Python ImportError: No module named Image
/********************************************************************************* * Python ImportEr ...
- IO综合练习--文件切割和文件合并
有时候一个视频文件或系统文件太大了,上传和下载可能会受到限制,这时可以用文件切割器把文件按大小切分为文件碎片, 等到要使用这个文件了,再把文件碎片合并成原来的文件即可.下面的代码实现了文件切割和文件合 ...
- [CSAcademy]Colored Forests
csacademy description 有\(M\)种颜色编号为\(1-M\).现给树上的每个点染上这\(M\)种颜色中的一种,定义一棵树是\(\mbox{colorful}\)的当且仅当这棵树上 ...