[LeetCode] Implement Queue using Stacks

简介: A classic interview question. This link has a nice explanation of the idea using two stacks and its amortized time complexity.

A classic interview question. This link has a nice explanation of the idea using two stacks and its amortized time complexity.

I use the same idea in my code, which is as follows.

 1 class Queue {
 2 public:
 3     // Push element x to the back of queue.
 4     void push(int x) {
 5         stack1.push(x);
 6     }
 7 
 8     // Removes the element from in front of queue.
 9     void pop(void) {
10         if (stack2.empty()) {
11             while (!stack1.empty()) {
12                 int elem = stack1.top();
13                 stack1.pop();
14                 stack2.push(elem);
15             }
16         }
17         stack2.pop();
18     }
19 
20     // Get the front element.
21     int peek(void) {
22         if (stack2.empty()) {
23             while (!stack1.empty()) {
24                 int elem = stack1.top();
25                 stack1.pop();
26                 stack2.push(elem);
27             }
28         }
29         return stack2.top();
30     }
31 
32     // Return whether the queue is empty.
33     bool empty(void) {
34         return stack1.empty() && stack2.empty();
35     }
36 private:
37     stack<int> stack1;
38     stack<int> stack2;
39 };

 

目录
相关文章
|
存储
LeetCode 232. Implement Queue using Stacks
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。
73 0
LeetCode 232. Implement Queue using Stacks
LeetCode 225. Implement Stack using Queues
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈; pop() -- 移除栈顶元素; top() -- 获取栈顶元素; empty() -- 返回栈是否为空
79 0
LeetCode 225. Implement Stack using Queues
LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement the following operations of a stack using queues.
1068 0
LeetCode 232:用栈实现队列 Implement Queue using Stacks
题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。
666 0
|
算法 Java C语言
LeetCode 28:实现strStr() Implement strStr()
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 实现 strStr() 函数。 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始)。
694 0
|
算法 Java C++
LeetCode 208 Implement Trie (Prefix Tree)(实现前缀树)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51619848 翻译 实现一个包含insert,search和startsWith方法的前缀树。
1311 0
|
算法
LeetCode - 28. Implement strStr()
28. Implement strStr()  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定两个字符串str1和str2,输出str2在str1中第一次出现的下标.
924 0
|
算法
LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50550978 翻译 用栈来实现队列的下列操作。
668 0
|
算法
LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50551035 翻译 用队列来实现栈的如下操作。
1375 0