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()

 

posted @ 2017-09-09 16:31  HitAnyKey  阅读(190)  评论(0编辑  收藏  举报