LeetCode Implement Stack using Queues (数据结构)
2024-10-21 15:59:11
题意:
用队列来实现栈。
思路:
没有什么捷径,纯粹模拟。但是用一个队列就够了。
class Stack {
/*
// Push element x onto stack.
void push(int x) { } // Removes the element on top of the stack.
void pop() { } // Get the top element.
int top() { } // Return whether the stack is empty.
bool empty() { }
*/
queue<int> que;
public:
void push(int x)
{
que.push(x);
}
void pop(void)
{
if(!que.empty())
{
int n=que.size(),pos=;
while(pos<n)
{
que.push(que.front());
que.pop();
pos++;
}
que.pop();
}
}
int top()
{
if(!que.empty())
{
int n=que.size(), pos=,x=;
while(pos<=n)
{
if(pos==n) x=que.front();
que.push(que.front());
que.pop();
pos++;
}
return x;
}
}
bool empty(void)
{
if(que.empty()) return true;
else return false;
}
};
AC代码
最新文章
- Chp11 11.7
- 修复 XE8 for Android 分享图片到 Gmail 权限不足的问题
- 重新想象 Windows 8.1 Store Apps (92) - 其他新特性: CoreDispatcher, 日历, 自定义锁屏系列图片
- 设置presentVC跟PushVC一样的效果即从右到左的动画
- [译]Cassandra的数据读写与压缩
- AIX topas命令详解
- C#结构内存布局介绍
- svn笔记2
- C# .Net 使用zxing.dll生成二维码,条形码
- iOS8 UISearchViewController搜索功能讲解
- JavaScript模块化 --- Commonjs、AMD、CMD、es6 modules
- The authenticity of host &#39;github.com (192.30.253.113)&#39; can&#39;t be established.
- C# Winform无边框窗口拖动
- 痞子衡嵌入式:飞思卡尔Kinetis开发板OpenSDA调试器那些事(上)- 背景与架构
- echarts柱状图点击阴影部分触发事件
- Windows上安装 TensorFlow及简单命令
- AMH 软件目录介绍
- linux eclipse 报错过时的方法
- 我的第一个HTML5应用
- bootstrap 多级下拉菜单