1、两个队列实现,始终保持一个队列为空即可

 class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
if(que1.empty())
{
que1.push(x);
while(!que2.empty())
{
int val=que2.front();
que2.pop();
que1.push(val);
}
}
else
{
que2.push(x);
while(!que1.empty())
{
int val=que1.front();
que1.pop();
que2.push(val);
}
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
if(que1.empty())
{
int val=que2.front();
que2.pop();
return val;
}
if(que2.empty())
{
int val=que1.front();
que1.pop();
return val;
}
} /** Get the top element. */
int top() {
if(que1.empty())
return que2.front();
if(que2.empty())
return que1.front();
} /** Returns whether the stack is empty. */
bool empty() {
return que1.empty()&&que2.empty();
}
private:
queue<int> que1;
queue<int> que2;
}; /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/

2、一个队列实现栈

 class MyStack {
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
que.push(x);
for(int i=;i<que.size()-;++i)
{
que.push(que.front());
que.pop();
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int val=que.front();
que.pop();
return val;
} /** Get the top element. */
int top() {
return que.front();
} /** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
private:
queue<int> que;
}; /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/

LeetCode 225 Implement Stack using Queues 用队列实现栈的更多相关文章

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

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

  2. 225 Implement Stack using Queues(用队列实现栈Medium)

    题目意思:用队列实现栈,push(),pop(),top(),empty() 思路:用两个queue,pop时将一个queue的元素pop再push到另一个队列,queue只留最后一个元素,并pop, ...

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

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

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

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

  5. LeetCode OJ:Implement Stack using Queues(队列实现栈)

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

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

  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 [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

  9. Implement Stack using Queues 用队列实现栈

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

随机推荐

  1. bzoj 2093 [Poi2010]Frog——滑动窗口

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2093 找第k近的可以用一个含k个元素的滑动窗口来实现. 卡空间也还行,但卡时间.不要预处理倍 ...

  2. 人物-IT-刘强东:刘强东

    ylbtech-人物-IT-刘强东:刘强东 刘强东,男,汉族,1973年3月10日生(另一说法:1974年2月14日),江苏宿迁人,祖籍湖南湘潭 .京东集团董事局主席兼首席执行官,本科毕业于中国人民大 ...

  3. Python命令模块argparse学习笔记(四)

    默认参数 ArgumentParser.set_defaults(**kwargs) set_defaults()可以设置一些参数的默认值 >>> parser = argparse ...

  4. 大内存电脑在vbox安装linux报错

    问题描述: 1.机器:Linux主机,特别是主机为大内存,比如: 4G内存的使用pae内核的Ubuntu系统的thinkpad电脑. 2.情况:使用VirtualBox安装Linux系统时,比如:通过 ...

  5. mysql--二进制日志(bin-log)

    一.设置二进制日志 进制日志记录了所有的DDL和DML,但不包括各种查询.通过二进制日志,可以实现什么效果呢?二进制日志文件可以[实现灾难数据恢复],另外可以应用到[mysql复制数据同步].二进制日 ...

  6. “box-shadow”属性(转)

    “box-shadow”属性 box-shadow属性是一个CSS3属性,允许我们在几乎任何元素上来创建阴影效果,类似于在设计软件中的”drop shadow”.这些阴影效果允许我们在原本平面的.二维 ...

  7. 从Github远程库安装Node.JS

    3)从Github远程库安装Node.JS在这个方法中我们需要一些步骤来把Node.js的从Github上的远程的仓库克隆到本地仓库目录 在开始克隆(克隆)包到本地并且配制之前,我们要先安装以下依赖包 ...

  8. spring 4.0 JUnit简单的Dao,Service测试

    1.AbstractTransactionalJUnit4SpringContextTests 和AbstractJUnit4SpringContextTests.我们在测试用例类要继承两种中的一个. ...

  9. PCL 不同类型的点云之间进行类型转换

    PCL 不同类型的点云之间进行类型转换 可以使用PCL里面现成的函数pcl::copyPointCloud(): #include <pcl/common/impl/io.h> pcl:: ...

  10. R: which(查询位置)、%in% (是否存在)、ifelse(判断是否):

    ################################################### 问题:ifelse.which.%in%    18.4.27 解决方案: > x < ...