▶ 栈和队列的相互表示。发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小。

▶ 第 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的更多相关文章

  1. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  2. 【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( ...

  3. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  4. 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 ...

  5. 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) -- ...

  6. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

  7. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. 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 ...

  9. (easy)LeetCode 225.Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

随机推荐

  1. Android之第三方平台实现QQ登录和QQ分享

    目前大多数APP都包含了第三方平台的登录,特别是QQ和微信,这篇博客主要讲的是如何实现QQ第三方平台实现QQ登录和分享功能,功能包含: 登录授权登录获取用户信息(昵称,头像,地址等) QQ分享给好友 ...

  2. 个人知识管理系统Version1.0开发记录(04)

    demo model 我们采用mvc软件架构模式,方便以后用Struts2框架技术优化.重构.封装.这次主要设计一些常用的方法工具,即数据访问逻辑.工具:eclipse.oracle.sqldevel ...

  3. IOS-static cell 与 dynamic cell 混合使用

    static cell 与 dynamic cell 混合使用 关于静态cell与动态cell的混合使用,google一下便会有很多相关文章,这里也是看过一些前辈的经验(已经忘记具体是从哪篇文章得到的 ...

  4. Android学习必备--java工具15个

    Weka .Weka集成了数据挖掘工作的机器学习算法.这些算法可以直接应用于一个数据集上或者你可以自己编写代码来调用.Weka包括一系列的工具,如数据预处理.分类.回归.聚类.关联规则以及可视化. M ...

  5. 使用springfox+swagger2书写API文档(十八)

    使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...

  6. iOS语法糖 简单却不那么简单

    转载作者 香蕉大大 (Github) 开发过程中我特别喜欢用语法糖,原因很简单,懒得看到一堆长长的代码,但是语法糖我今天无意中看到更有意思的玩法.这里暂时吧把今天新学到的知识点整理一下希望大家喜欢,如 ...

  7. CF1056:Check Transcription(被hack的hash)

    One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a ...

  8. 20155211 2016-2017-2 《Java程序设计》第八周学习总结

    20155211 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章 NIO与NIO2 NIO使用频道(channel)来衔接数据节点,对数据区的标记提 ...

  9. 汉诺塔(Hanoi)——小小算法

    传送门: 袁咩咩的小小博客 汉诺(Hanoi)塔源于古印度,是非常著名的智力趣题,大意如下: 勃拉玛是古印度的一个开天辟地的神,其在一个庙宇中留下了三根金刚石的棒,第一 根上面套着64个大小不一的圆形 ...

  10. 树莓派(raspberry pi)系统开发

    [树莓派(raspberry pi)] 01.在linux环境下给树莓派安装系统及入门各种资料 [树莓派(raspberry pi)] 02.PI3安装openCV开发环境做图像识别(详细版) 出处: ...