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. Maven(6)-POM

    to be continued.

  2. MD5 校验文件

    https://blog.csdn.net/wudishine/article/details/42466831 MD5.h #ifndef MD5_H #define MD5_H #include ...

  3. UML核心元素--包

    包是一种容器,如同文件夹一样,将某些信息分类,形成逻辑单元.包可以容纳任何UML元素,例如用例.业务实体.类图等,也包括子包. 一.分包原则: (1)高内聚:被分入同一个包的元素相互联系紧密,伸至不可 ...

  4. 【jQuery】jquery.metadata.js验证失效

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. CentOS7下二进制文件安装MySQL5.6

    1.查看已装包 [root@host2 ~]# rpm -qa | grep mysql mysql-libs-5.1.71-1.el6.x86_64 [root@host2 ~]# [root@ho ...

  6. vmware克隆Centos网卡修改方法

    vmware克隆Centos网卡修改方法 1,查看网卡信息,获得eth编号和MAC地址 # dmesg | grep eth  e1000 0000:02:00.0:eth0:(PCI:66MHz:3 ...

  7. 文件格式——fasta格式

    fasta格式 在生物信息学中,FASTA格式(又称为Pearson格式),是一种基于文本用于表示核苷酸序列或氨基酸序列的格式.在这种格式中碱基对或氨基酸用单个字母来编码,且允许在序列前添加序列名及注 ...

  8. 6.7 通过命令启动IDEA,给IDEA创建快捷方式

    我的IDEA安装在这里: 打开终端: 这里的./表示当前路径.如果是/表示根目录. 通过这个命令行,就可以开启IDEA了. 接下来,给IDEA创建快捷方式. 工具栏Tools->create d ...

  9. Spring入门第二十六课

    Spring中的事务管理 事务简介 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性. 事务就是一系列的动作,他们被当做一个单独的工作单元,这些动作要么全部完成,要么全部不起 ...

  10. 4. DVWA亲测暴力破解

    LOW等级 我们先用burpsuite抓包,因为burpsuite提供了暴力破解模块   我们先创建一个1.txt文件夹,把正确的账号密码写进去   我们输入 Username:1 Password: ...