用队列实现栈。这个实现方法十分的简单,就是在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的get、post请求

    URLConnection package com.shuzf.http; import java.io.BufferedReader; import java.io.IOException; imp ...

  2. SSH服务器拒绝了密码,请再试一次

    使用Xshell连接ubuntu后,出现: SSH服务器拒绝了密码,请再试一次! 输入: cd /etc/ssh/ 继续: vim sshd_config 若此时提示没有安装vim,那我们安装以下: ...

  3. 关于mysql中的count()函数

    1.count()函数是用来统计表中记录的一个函数,返回匹配条件的行数. 2.count()语法: (1)count(*)---包括所有列,返回表中的记录数,相当于统计表的行数,在统计结果的时候,不会 ...

  4. 软件工程(FZU2015) 赛季得分榜,第一回合

    SE_FZU目录:1 2 3 4 5 6 7 8 9 10 11 12 13 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分=团队得分+个人贡献分 个人贡献分: 个人 ...

  5. 如何给框架添加API接口日志

    前言 用的公司的框架,是MVC框架,看了下里面的日志基类,是操作日志,对增删改进行记录, 夸张的是一张业务的数据表 需要一张专门的日志表进行记录, 就是说你写个更新,添加的方法都必须写一遍操作日志,代 ...

  6. 使用Gradle构建web工程配置详解

  7. springboot的热部署

    SpringBoot 4.SpringBoot 整合 devtools 实现热部署   一.添加 devtools 依赖 <!-- Spring boot 热部署 : 此热部署会遇到 java. ...

  8. 安装mysql8.0出现服务无法启动,服务没报告任何错误

    改为net start mysql80 参考https://blog.csdn.net/gzejia/article/details/82156994

  9. Ubuntu 16.04安装MySQL(5.7.18)

    此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 安装MySQL前需要做如下了解: 1.MySQL各类型版本的区别,参考:http://ww ...

  10. java json转换(二)

    package com.kps.common.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArra ...