LeetCode225 Implement Stack using Queues
Implement the following operations of a stack using queues. (Easy)
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
分析:
使用两个队列实现,其中一个队列为空,用来进行颠倒顺序和输出队尾元素。
入栈操作: 向非空队列内入队即可
出栈操作:将非空队列除队尾元素外的所有元素导入另一个空队列,剩余队尾元素即为待应该待出栈元素
top()操作: 同出栈,注意只需访问返回,不需要让其出队,即仍需将其导入另一队列
注意:两队列地位平等,均可能用作储存和转移工作
代码:
- class Stack {
- private:
- queue<int> q1,q2;
- public:
- // Push element x onto stack.
- void push(int x) {
- if(!q1.empty()){
- q1.push(x);
- }
- else{
- q2.push(x);
- }
- }
- // Removes the element on top of the stack.
- void pop() {
- if(!q1.empty()){
- while(q1.size() > ){
- q2.push(q1.front());
- q1.pop();
- }
- q1.pop();
- }else{
- while(q2.size() > ){
- q1.push(q2.front());
- q2.pop();
- }
- q2.pop();
- }
- }
- // Get the top element.
- int top() {
- if(!q1.empty()){
- while(q1.size() > ){
- q2.push(q1.front());
- q1.pop();
- }
- int ans = q1.front();
- q2.push(q1.front());
- q1.pop();
- return ans;
- }else{
- while(q2.size() > ){
- q1.push(q2.front());
- q2.pop();
- }
- int ans = q2.front();
- q1.push(q2.front());
- q2.pop();
- return ans;
- }
- }
- // Return whether the stack is empty.
- bool empty() {
- if(q1.empty() && q2.empty()){
- return true;
- }
- return false;
- }
- };
LeetCode225 Implement Stack using Queues的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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 ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- ECMAScript 5 新增 Object 接口
对象 构造器 说明 Object getPrototypeOf 返回对象的原型 Object getOwnPropertyDescriptor 返回对象自有属性的属性描述符 Object getOwn ...
- python-基础-字符串-列表-元祖-字典2
接上:http://www.cnblogs.com/liu-wang/p/8973273.html 3 元组 4 字典 4.1 字典的介绍 <2>软件开发中的字典 变量info为字典类型: ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
- 常用web字体的使用指南
而真正的挑战在于中文字体,由于中文字体组成的特殊性导致其体积过于庞大,除了操作系统内置的字体之外,我们很难在网站上应用其他的字体.在可选性很差的前提之下,如何正确的使用中文字体呢? 首先,以下的字体声 ...
- 关于JavaScript的43道题①
最近在github上大火的43到js代码题,有很多人搬运.原链接https://github.com/lydiahallie/javascript-questions 1.下面代码的输出是什么? fu ...
- 使用setTimeout函数解决栈溢出问题
下面的代码,如果队列太长会导致栈溢出,怎样解决这个问题并且依然保持循环部分: var list = readHugeList(); var nextListItem = function() { va ...
- Hackerrank--Savita And Friends(最小直径生成树MDST)
题目链接 After completing her final semester, Savita is back home. She is excited to meet all her friend ...
- VMware 安装 ubuntu 后安装 VMWare tools
1.如果 VMware 的安装 VMWare tools 的菜单是灰色, 很可能原因是: 你的 cdrom 被占用着. 关闭系统, 编辑配置, 把cdrom 改为 自动检测. 即不要开始就加载一 ...
- Oracle中函数如何返回结果集
在Oracle中,用函数返回结果集有时候要用到,下面是demo: 1 2 3 4 5 6 7 create or replace type t_test as object ( id integer, ...
- python模拟浏览器文件上传,csrf放行
服务器端视图函数 from django.shortcuts import render,HttpResponse from django.views.decorators.csrf import c ...