【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Implement the following operations of a stack using queues.
- 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, and is 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)
(二)解题
题目大意:用queue来实现一个stack。
解题思路:queue是先进先出,stack是先进后出。采用一个queue就能实现stack
push:直接压入到queue
pop:需要弹出queue的尾元素,可是queue只能弹出queue的头元素。这时候可以把queue的头元素弹出再压入queue,一直到尾元素弹出即可
top:直接利用queue的back()即可知道stack顶元素
empty:判断queue是否为空。
class Stack {
public:
// Push element x onto stack.
void push(int x) {
que.push(x);
}
// Removes the element on top of the stack.
void pop() {
int size = que.size();
for(int i = 0 ; i < size-1 ;i++){//queue的头元素弹出再压入,直到尾元素弹出
que.push(que.front());
que.pop();
}
que.pop();
}
// Get the top element.
int top() {
return que.back();
}
// Return whether the stack is empty.
bool empty() {
if(que.empty()) return true;
return false;
}
private:
queue<int> que;
};
【一天一道LeetCode】#225. Implement Stack using Queues的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. 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 ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- hdu 5014(贪+位运算)
题意:给你n+1个数(0->n),让你为这n+1个数在0->n中分别找一个数与其异或,求最后的最大值 思路:假设一个数5 (二进制1 0 1),则找的另一个数在5的0位上最好是1 , 1位 ...
- 【Uva 11280 飞到弗雷德里顿】
·你可以尽情地坐飞机,但停留次数遭到限制. ·英文题,述大意: 给出一张有向图,起点是输入的第一个城市,终点是输入的最后一个城市.给出q个询问,每个询问含一个t,表示 #include&l ...
- bzoj2823[AHOI2012]信号塔
2823: [AHOI2012]信号塔 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1190 Solved: 545[Submit][Status ...
- hihiocoder 1255(搜索)(2015ACM/ICPC北京站)
题意: 给你四个矩形,判断能否从中选出3个组成一个矩形 思路: 1.搜索,如果两个能组成一个新的,则将他们合并,继续搜索 2.暴力判断 最开始没注意到3,一直以为要用4个,WR #include &l ...
- bzoj3702二叉树 线段树合并
3702: 二叉树 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 600 Solved: 272[Submit][Status][Discuss] ...
- day4 liaoxuefeng---函数式编程
一.概述: 函数式编程的一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数! Python对函数式编程提供部分支持.由于Python允许使用变量,因此,Python不是纯函数式编程 ...
- Linux文件系统的介绍
1.Linux的文件系统是一个典型的树形结构,只有一个根节点 如下图: 2.在Linux中一切皆文件 Linux 对数据文件(.mp3..bmp),程序文件(.c..h.*.o),设备文件(LCD.触 ...
- c++中双冒号的作用
双冒号(::)用法 参考链接:https://segmentfault.com/a/1190000000345680 1.表示“域操作符”例:声明了一个类A,类A里声明了一个成员函数void f(), ...
- POJ-2299 Ultra-QuickSort---树状数组求逆序对+离散化
题目链接: https://vjudge.net/problem/POJ-2299 题目大意: 本题要求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序. 两个数(a, ...
- java判断A字符串是否包含B字符串
public static void main(String[] args) { String str="ABC_001"; if(str.indexOf("ABC&qu ...