用队列实现栈。这个实现方法十分的简单,就是在push这一步的时候直接变成逆序。

class MyStack {
private:
queue<int> q;
queue<int> q2;
public:
/** Initialize your data structure here. */
MyStack() { } /** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i=;i<q.size()-;i++)
{
q.push(q.front());
q.pop();
}
} /** Removes the element on top of the stack and returns that element. */
int pop() {
int a = q.front();
q.pop();
return a;
} /** Get the top element. */
int top() {
return q.front();
} /** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
}; /**
* 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();
*/

【easy】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 ...

  2. 【LeetCode】225. Implement Stack using Queues 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【一天一道LeetCode】#225. Implement Stack using Queues

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

  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❤python】 225. Implement Stack using Queues

    #-*- coding: UTF-8 -*-class Stack(object):    def __init__(self):        """        i ...

  6. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

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

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

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

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

随机推荐

  1. Java HttpURLConnection发送post请求示例

    public static Map<String, Object> invokeCapp(String urlStr, Map<String, Object> params) ...

  2. hdu-1728(贪心&&bfs的灵活运用吧)

    链接 [https://vjudge.net/contest/256476#problem/D] 题意 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到 ...

  3. PS快速调出天蓝色清新外景

    原片: 一.调整光比 曝光 黑白灰. 二.调整色温(新手可用白平衡工具.左上角第3个)调整饱和度(自然饱和度和蓝原色) 三.互补色的运用(高光偏黄 加的蓝色 暗部发蓝青色 加的橙黄色) 四.调整好照片 ...

  4. [系统软件]Ubuntu 18.04 LTS 安装 搜狗输入法,谷歌拼音

    1. 讲什么 本文主要讲述在Ubuntu18.04 LTS版本中安装搜狗输入法.谷歌拼音输入法的过程. 2. 为什么讲 1. Ubuntu电脑自带Ibus输入法+拼音/五笔,但是用了一段时间之后发现经 ...

  5. web 日历 任务 插件

    [2] 日历任务插件(jquery版)新增日历任务和点击监听 - qq_26462567的博客 - CSDN博客https://blog.csdn.net/qq_26462567/article/de ...

  6. Mac之brew使用

    brew : 终端程序管理工具 能让你更快速的安装你想要的工具.而不用考虑大量的依赖. 安装命令 给官网的一样也可以自己去官网查看 它就类似于centos下的yum 和 Ubuntu下的apt-get ...

  7. Shell命令-系统信息及显示之stat、du

    文件及内容处理 - stat.du 1. stat:显示inode内容 stat命令的功能说明 stat 命令用于显示 inode 内容.stat 以文字的格式来显示 inode 的内容. stat命 ...

  8. Git里有些费解的术语和设计

    关于暂存区, 好几个地方都写到了 正在编辑的文件 --> Unchacked/Modified, 而Unchacked/Modified, 的状态也可以叫 to be committed . 这 ...

  9. less的基本使用

    众所周知,less是一门css预处理语言,其他的类似还有scss.Stylus 等,和js相似度比较高的就是less了.话不多说,我们来看less的使用. Node.js 环境中使用 Less : n ...

  10. SQL 中使用 WITH AS 提高性能

    一.WITH AS的含义WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让S ...