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 ...
随机推荐
- Coderforce 560B-Gerald is into Art
题目大意:给了三个矩形的长和宽,问第一个能否把其他两个装在内部,要求内部之间不重叠,不出界(可重边)? 题目分析:这道题...考虑不够全面导致比赛时没有出来...当时,就是觉得自己的代码很完美,不可能 ...
- jQuery - 左右拖动分隔条
1.实现效果: 2.代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...
- Centos安装Chrome浏览器失败解决办法
最近因为项目需要使用到Centos,自己经常使用Chrome,所有的书签以及信息都是同步在Google,所以尝试在Centos上安装Chrome,按照网上的资料都是安装失败,显示缺少资源,不过最终还是 ...
- Build Tree View Structure for SharePoint List Data
博客地址 http://blog.csdn.net/foxdave 此文参考自->原文链接 版权归原作者所有,我只是进行一下大致的翻译 应坛友要求,帮助验证一下功能. SharePoint列表数 ...
- Scrum立会报告+燃尽图(3)选题
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2193 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...
- node 应用集合
node+react上传 淘宝的formidable express部署
- js中的reduce()函数
1. 首先看下语法如下 2 . 写了个demo如下 var fa = [1,2,3,4] function red(a, b) { console.log(arguments); return a + ...
- 使用peach工具进行fuzz测试
本文简要介绍了Fuzz 工具Peach的使用,并通过文件格式 Fuzz举例阐述了 Peach Pit 文件的编写. 本文转自“绿盟科技博客”:http://blog.nsfocus.net/peach ...
- iOS-----使用AddressBookUI管理联系人
使用AddressBookUI管理联系人 iOS SDK为管理地址簿提供的视图控制器位于AddressBookUI框架内.总结来说,AddressBookUI框架提供了如下特殊的视图控制器. ABPe ...
- It is the courage
It is the reality that a society which becomes lower and becomes weak.Believe it or not,I think it i ...