[LeetCode]232. Implement Queue using Stacks
2017-04-15 20:28
295 查看
[LeetCode]232. Implement Queue using Stacks
题目描述
思路
两个栈实现代码
#include <iostream> #include <stack> using namespace std; class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { input.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { int res = peek(); if (output.size()) { output.pop(); } return res; } /** Get the front element. */ int peek() { if (output.empty()) { while (input.size()) { output.push(input.top()); input.pop(); } } if(output.size()) return output.top(); return 0; } /** Returns whether the queue is empty. */ bool empty() { return input.empty() && output.empty(); } private: stack<int> input, output; }; int main() { MyQueue obj; int param_2 = obj.pop(); int param_3 = obj.peek(); bool param_4 = obj.empty(); cout << param_2 << " " << param_3 << " " << param_4 << endl; system("pause"); return 0; }
相关文章推荐
- leetcode(64).232. Implement Queue using Stacks
- [LeetCode]--232. Implement Queue using Stacks
- [leetcode]232. Implement Queue using Stacks
- leetcode 232. Implement Queue using Stacks
- LeetCode 232. Implement Queue using Stacks
- Leetcode 232. Implement Queue using Stacks
- leetcode 225|232. Implement Stack using Queues 232. Implement Queue using Stacks
- leetcode-标签为stack 232. Implement Queue using Stacks
- 【leetcode】232. Implement Queue using Stacks
- Leetcode 232. Implement Queue using Stacks
- Leetcode 232. Implement Queue using Stacks
- leetcode 232. Implement Queue using Stacks
- LeetCode 232. Implement Queue using Stacks
- Leetcode232. Implement Queue using Stacks
- leetcode 232. Implement Queue using Stacks
- leetcode 232. Implement Queue using Stacks
- LeetCode 232. Implement Queue using Stacks(用栈实现队列)
- [LeetCode] 232. Implement Queue using Stacks
- 232. Implement Queue using Stacks LeetCode
- leetcode 232. Implement Queue using Stacks