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. android获取时间差的方法

    本文实例讲述了android获取时间差的方法.分享给大家供大家参考.具体分析如下: 有些时候我们需要获取当前时间和某个时间之间的时间差,这时如何获取呢? 1. 引用如下命名空间: import jav ...

  2. Poj_1045

    这道题难点在于基本物理知识和数学的结合. 得出公式后再code,那就是小菜一碟了. import java.util.Scanner; import java.lang.Math; public cl ...

  3. node.js的国内源

    node.js在使用npm安装包是,由于源是国外的,有可能会被GFW屏蔽. 通过下面的方法可以把源指向国内的. 具体方法如下: 编辑 ~/.npmrc 加入下面内容 registry = http:/ ...

  4. 关于日志类Log4j的使用

    log4j 的配置 #下面定义日志输出级别是 INFO,并且配置了2个输出目的地,一个是A3,一个是console log4j.rootLogger = INFO,A3,CONSOLE #日志最低的输 ...

  5. 【转】 Pro Android学习笔记(三八):Fragment(3):基础小例子-续

    目录(?)[-] Step 2实现Fragment指定调用类TitleFragment onInflate和onAttach onCreate和onCreateView onActivityCreat ...

  6. JavaScript-Tool:jquery.cookie.js

    ylbtech-JavaScript-Tool:jquery.cookie.js 1.返回顶部 1.jquery.cookie.js /*! * jQuery Cookie Plugin v1.4.0 ...

  7. 二 Akka学习 - actor介绍

    一个actorSystem 是一个重量级的结构.它会分配N个线程.所以对于每一个应用来说只用创建一个ActorSystem. Actor是种可怜的“生物”,它们不能独自存活.Akka中的每一个Acto ...

  8. 使用python对文件夹里面所有代码行数进行统计。

    统计目录下所有的代码个数和总行数. # -*- coding: utf-8 -*- # @Author : ydf import json import os from pathlib import ...

  9. 实现reentrantlock和读写锁

    1 可以手动实现一个类似reentrantlock的工具,首先要维护一个state的标志,代表当前是否有线程已经使用资源.线程lock的时候, 会用cas给state加1,其他线程检测状态.另外需要维 ...

  10. 13、ubuntu终端快捷键(参考 dy9776)

    1.终端的快捷键 Tab 自动补全 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 Ctrl+l 相当于clear,即清屏 Ctrl+Z 把当前任务放到后台运行(相当于运行命令时后面 ...