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

▶ 第 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. 剑指 offer面试题22 栈的压入和弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...

  2. springboot模糊查询

    在学习MyBatis过程中想实现模糊查询,可惜失败了.后来上百度上查了一下,算是解决了.记录一下MyBatis实现模糊查询的几种方式. 数据库表名为test_student,初始化了几条记录,如图: ...

  3. oracle修改约束列

    Oracle 增加修改删除字段 添加字段的语法:alter table tablename add (column datatype [default value][null/not null],-. ...

  4. Transaction ACID (转载)

    Transaction 原文出处: 黄勇    Transaction 也就是所谓的事务了,通俗理解就是一件事情.从小,父母就教育我们,做事情要有始有终,不能半途而废.�0�2事务也是这样,不能做一般 ...

  5. python线程的GIL问题(全局解释器锁)

    造成原因: python ---> 支持线程操作 --->IO的同步和互斥 --> 加锁 ----> 超级锁,给解释器加锁--->解释器同一时刻只能解释一个线程 造成的后 ...

  6. BZOJ5090: [Lydsy1711月赛]组题(01分数规划)

    5090: [Lydsy1711月赛]组题 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 785  Solved: 186[Submit][Status ...

  7. 关于cookie和session的使用和理解

    由于项目需要,最近用session容器比较多,传载的同时加上了自己的一些理解,不足之处还请大家补充和纠正. 一.cookie机制和session机制的区别 ********************** ...

  8. HDU2888 Check Corners(二维RMQ)

    有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ #pragma comment(linker, "/STACK:1677721600&qu ...

  9. web本地存储(localStorage、sessionStorage)

    web 本地存储 (localStorage.sessionStorage) 说明 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:l ...

  10. USB相关的sysfs文件

    主要来自driver/usb/core/sysfs.c: 1.bConfigurationValue RW,W时调用了usb_set_configuration()实时设置配置.根据USB规范(例如第 ...