Implement Queue using Stacks(用两个栈实现队列)

来源:https://leetcode.com/problems/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.

Notes:

  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is emptyoperations 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).

栈A负责push,栈B负责pop和peek,当pop或peek时,如果栈B没有元素,就将栈A中的内容按栈的pop顺序push到栈B中

Java

 1 class MyQueue {
 2     Stack<Integer> input;
 3     Stack<Integer> output;
 4     /** Initialize your data structure here. */
 5     public MyQueue() {
 6         input = new Stack<Integer>();
 7         output = new Stack<Integer>();
 8     }
 9
10     /** Push element x to the back of queue. */
11     public void push(int x) {
12         input.push(x);
13     }
14
15     /** Removes the element from in front of queue and returns that element. */
16     public int pop() {
17         peek();
18         return output.pop();
19     }
20
21     /** Get the front element. */
22     public int peek() {
23         if(output.empty()) {
24             int len = input.size();
25             for(int i=0; i<len; i++) {
26                 output.push(input.pop());
27             }
28         }
29         return output.peek();
30     }
31
32     /** Returns whether the queue is empty. */
33     public boolean empty() {
34         return input.empty() && output.empty();
35     }
36 }
37
38 /**
39  * Your MyQueue object will be instantiated and called as such:
40  * MyQueue obj = new MyQueue();
41  * obj.push(x);
42  * int param_2 = obj.pop();
43  * int param_3 = obj.peek();
44  * boolean param_4 = obj.empty();
45  */

Python

 1 class MyQueue(object):
 2     i_stack = []
 3     o_stack = []
 4     def __init__(self):
 5         """
 6         Initialize your data structure here.
 7         """
 8         self.i_stack = []
 9         self.o_stack = []
10
11     def push(self, x):
12         """
13         Push element x to the back of queue.
14         :type x: int
15         :rtype: void
16         """
17         self.i_stack.append(x)
18
19     def pop(self):
20         """
21         Removes the element from in front of queue and returns that element.
22         :rtype: int
23         """
24         self.peek()
25         return self.o_stack.pop()
26
27     def peek(self):
28         """
29         Get the front element.
30         :rtype: int
31         """
32         if len(self.o_stack) == 0:
33             i_len = len(self.i_stack)
34             for i in range(i_len):
35                 self.o_stack.append(self.i_stack.pop())
36         return self.o_stack[-1]
37
38     def empty(self):
39         """
40         Returns whether the queue is empty.
41         :rtype: bool
42         """
43         return len(self.i_stack) == 0 and len(self.o_stack) == 0
44
45
46 # Your MyQueue object will be instantiated and called as such:
47 # obj = MyQueue()
48 # obj.push(x)
49 # param_2 = obj.pop()
50 # param_3 = obj.peek()
51 # param_4 = obj.empty()
时间: 2024-11-28 23:25:03

Implement Queue using Stacks(用两个栈实现队列)的相关文章

[leedcode 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. Notes: You

[CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Implement Queue using Stacks 用栈来实现队列.

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i

【LeetCode】232. Implement Queue using Stacks

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 t

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

Lintcode: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is

[LeetCode][JavaScript]Implement Queue using Stacks

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 t

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

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 whet

java-57-用两个栈实现队列&amp;&amp;用两个队列实现一个栈

转自:http://bylijinnan.iteye.com/blog/1450125 ———————————————————————————————————————————— Java代码   import java.util.ArrayList; import java.util.List; import java.util.Stack; /* * Q 57 用两个栈实现队列 */ public class QueueImplementByTwoStacks { private Stack<

【干货】容器适配器实现两个栈模拟队列

用两个栈模拟队列的思想就是"倒水思想",这里我们用自定义类型模拟出线性表,再用线性表做容器实现栈的数据结构,最后用栈来实现队列,代码如下: #include<iostream> #include<string> #include<cassert> struct __TrueType//类型萃取 { bool Get() { return true; } }; struct __FalseType { bool Get() { return false