一:题目

二:上码

// class MyStack {// private://      queue<int> q;// public:
//     /** Initialize your data structure here. */
//     MyStack() {//     }//     /** Push element x onto stack. */
//     void push(int x) {//         //后进 先出  反转队列 将新元素插到 第一个
//         int size = q.size();//还没插入新元素之前
//         q.push(x);
//         while(size--)//保证插入的第一个元素在队列的首位
//         {//             int temp = q.front();
//             q.pop();
//             q.push(temp);//插入到队尾
//         }
//     }//     /** Removes the element on top of the stack and returns that element. */
//     int pop() {//           int popele = q.front();//访问栈顶元素
//           q.pop();//删除栈顶元素
//           return popele;
//     }//     /** 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();
//  */class MyStack {private:/**思路:这里我们不能用两个都队列(一个进,一个出)来模拟栈,queue1: 1 2 3 4 queue2: 4 3 2 1移除4 (即栈顶)此时queue1: queue2: 3 2 1如果再来俩元素的话queue1: 5 6queue2:3 2 1想要移除栈顶6的话(再将队列一的元素移到队列二)queue2:3 2 1 6 5(可以看出无法得到 栈顶元素 6)**/queue<int> q;public:/** Initialize your data structure here. */MyStack() {}//我们将每次入队的时候 将元素放到末尾,然后将其他元素按顺序出队排到其后面/** Push element x onto stack. */void push(int x) {int size = q.size();q.push(x);while(size--) {int temp = q.front();q.pop();q.push(temp);}}/** Removes the element on top of the stack and returns that element. */int pop() {int nums = q.front();q.pop();return nums;}/** Get the top element. */int top() {int nums = q.front();return nums;}/** 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();*/

leetcode225. 用队列实现栈相关推荐

  1. Leetcode--225. 用队列实现栈(Java)

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  2. Leetcode255用队列构造栈

    使用队列构造栈 题目链接:Leetcode225 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- ...

  3. 算法训练Day11 | LeetCode232. 用栈实现队列(模拟);225.用队列实现栈(模拟);20. 有效的括号(栈应用);1047. 删除字符串中的所有相邻重复项(栈应用)

    目录 LeetCode232.用栈实现队列 1. 思路 2. 代码实现 3. 复杂度分析 4. 思考 LeetCode225. 用队列实现栈 1. 思路 2. 代码实现 3. 复杂度分析 4. 思考 ...

  4. 栈和队列相互实现 (用队列实现栈/用栈实现队列) 超详细~

    1.栈和队列的简单介绍 1.1 栈的简单介绍 栈,一种数据结构,只能从一端进出,先进后出,操作方式主要有出栈和入栈.在Java中,一般使用Stack(已不推荐)和Deque(双端队列)实现. Java ...

  5. 代码随想录【day 10 栈与队列】| 232.用栈实现队列、 225. 用队列实现栈

    代码随想录[day 10 栈与队列]| 232.用栈实现队列. 225. 用队列实现栈 理论基础 LeetCode 232.用栈实现队列 题目链接:232.用栈实现队列 卡哥文解 视频讲解 解题思路( ...

  6. leetcode-225 队列实现栈

    使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 队列的特点:先入先出 栈的特点:后入先出 ...

  7. python实现栈的操作_python简单实现队列和栈push、pop操作

    栈: # -*- coding: utf-8 -*- #定义序列 lst=[] def pop(): if(len(lst)==0): print"栈为空","无法出栈& ...

  8. 【C++】STL队列和栈的使用

    C++的STL标准模板库提供了队列和栈的基本操作.下面通过两个demo分别介绍STL队列和STL栈的使用. Demo1:STL队列 [题目]卡片游戏(题目来自刘汝佳<算法竞赛入门>) 桌上 ...

  9. LeetCode 225. Implement Stack using Queues--用队列实现栈--C++解法

    LeetCode 225. Implement Stack using Queues–用队列实现栈–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在 ...

最新文章

  1. 长征五号复飞成功:史上最重最大,2020月岩采样火星探测都要靠它
  2. LeetCode 1048. 最长字符串链(哈希+DP)
  3. kali linux 2.0下安装ssh和metasploit
  4. Mac远程管理软件Royal TSX
  5. 如何更快通过BSCI审核
  6. iphone双卡双待神器android,荐好货:让iPhone变双卡双待神器大盘点
  7. 银行招聘计算机考试时间,秋季银行招聘考试时间是什么时候?
  8. rms 公式 有效值_有效值、真有效值、基波有效值、全有效值概念辨析
  9. php表格显示成绩,学生成绩表格展示
  10. 淘宝和微信的扫码登录实现原理
  11. CTrackThread::PreTranslateMessage(MSG* pMsg)
  12. win10开启自带移动热点以及解决无法设置移动热点,请打开WLAN问题
  13. ios 画线平滑_ios-iPhone平滑草图绘制算法
  14. arr.filter()
  15. Unity五子棋游戏设计 和简单AI实现(1)
  16. 研华PCI板卡开发(1)快速入门(1)
  17. 2、快速选择工具和魔棒工具
  18. PM成长日记第三话-那些年我们一起做过的项目
  19. 线性代数笔记3 - 向量组的线性相关性
  20. iOS-plist: iOS Keys

热门文章

  1. Android之解决APP奔溃重启导致Fragment白屏问题
  2. TCP协议之如何保证传输的可靠性
  3. 剑指offer之和为s的数组
  4. C和指针之字符串编程练习11(统计一串字符包含the的个数)
  5. linux网络编程之怎么配置好unp.h文件
  6. python3 最基本且简单的实现组合设计模式
  7. linux block设备,Linux I/O Block--块设备的表示
  8. java剪切txt文件_用Java把剪切板的内容实时保存到txt
  9. vscode搜索文件_VS Code 新图标来临 —— 侧边栏、文件管理器、搜索、调试等区域的图标迎来全新设计...
  10. c#队列取值_C# 队列