leetcode 225|232. Implement Stack using Queues 232. Implement Queue using Stacks
来源:互联网 发布:天族女捏脸数据导入图 编辑:程序博客网 时间:2024/06/09 15:16
225. Implement Stack using Queues
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.
- 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).
class MyStack {public: /** Initialize your data structure here. */ MyStack() {} /** Push element x onto stack. */ void push(int x) //重点就是push进去的时候花点时间,把前面所有的重新push一次 { int size = que.size(); que.push(x); for (int i = 0; i < size; i++) { que.push(que.front()); que.pop(); } } /** Removes the element on top of the stack and returns that element. */ int pop() { int key = que.front(); que.pop(); return key; } /** Get the top element. */ int top() { return que.front(); } /** Returns whether the stack is empty. */ bool empty() { return que.empty(); }private: queue<int> que;};/** * 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(); */
232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
使用两个stack
class MyQueue {public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { while (!sk.empty()) { tmp.push(sk.top()); sk.pop(); } sk.push(x); while (!tmp.empty()) { sk.push(tmp.top()); tmp.pop(); } } /** Removes the element from in front of queue and returns that element. */ int pop() { int ret = sk.top(); sk.pop(); return ret; } /** Get the front element. */ int peek() { return sk.top(); } /** Returns whether the queue is empty. */ bool empty() { return sk.empty(); }private: stack<int> sk; stack<int> tmp;};/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * bool param_4 = obj.empty(); */
阅读全文
0 0
- leetcode 225|232. Implement Stack using Queues 232. Implement Queue using Stacks
- leetcode 225 & 232.Implement Stack using Queues & Implement Queue using Stacks
- 232. Implement Queue using Stacks&225. Implement Stack using Queues
- 232. Implement Queue using Stacks && 225. Implement Stack using Queues
- Implement Stack using Queues && Implement Queue using Stacks (LeetCode)
- leetcode stack 232. Implement Queue using Stacks
- Implement Stack using Queues && Implement Queue using Stacks
- Implement Stack using Queues/Implement Queue using Stacks
- Stack----232. Implement Queue using Stacks
- LeetCode---Implement Queue Using Stacks、Implement Stack Using Queues解题分析
- leetcode-标签为stack 232. Implement Queue using Stacks
- [leetcode][stack] Implement Queue using Stacks
- [LeetCode]232.Implement Queue using Stacks
- 【leetcode】232. Implement Queue using Stacks
- <LeetCode OJ> 232. Implement Queue using Stacks
- 232. Implement Queue using Stacks LeetCode
- Leetcode: 232. Implement Queue using Stacks(JAVA)
- leetcode 232. Implement Queue using Stacks
- 揭秘黑客攻击内幕和20个黑客相关术语
- 如何提取文献图表中的散点数据,并python的matplotlib包重新绘制图片
- Redis集群搭建与简单使用
- 值得推荐的C/C++框架和库
- Qt--JSON
- leetcode 225|232. Implement Stack using Queues 232. Implement Queue using Stacks
- Java与Python命名规范
- python常用命令
- Android小技巧 Part I
- Jenkins上HTML报告显示无格式?
- linux内存管理机制以及free命令详解
- 关于使用fastdfs-client-java链接fastDFS(外网访问)超时问题
- 中英文混合按照首字母排序
- C# 写随机卡号和密码的方法