###### Implement Queue using Stacks
class MyQueue {
// Using two stacks.
Stack<Integer> s1 = new Stack<Integer>();// Pop out.
Stack<Integer> s2 = new Stack<Integer>();// Cache.

// Push element x to the back of queue.
public void push(int x) {
s2.push(x);
}

// Removes the element from in front of queue.
public void pop() {
if (empty()) {
return;
}
if (s1.isEmpty()) {
while (!s2.isEmpty()) {
s1.push(s2.peek());
s2.pop();
}
}
s1.pop();
}

// Get the front element.
public int peek() {
if (empty()) {
return -1;
}
if (s1.isEmpty()) {
while (!s2.isEmpty()) {
s1.push(s2.peek());
s2.pop();
}
}
return s1.peek();
}

// Return whether the queue is empty.
public boolean empty() {
return s1.isEmpty() && s2.isEmpty();
}
}

#### Leetcode_232_Implement Queue using Stacks

2015-09-12 11:01:23

#### 232. Implement Queue using Stacks [easy] (Python)

2016-06-04 23:32:59

#### LeetCode 232： Implement Queue using Stacks

2015-07-07 19:35:06

#### 【LeetCode-232】 Implement Queue using Stacks(C++)

2016-09-03 17:37:16

#### leetcode 232:Implement Queue using Stacks

2015-07-07 04:48:38

#### LeetCode232 Implement Queue using Stacks Java 题解

2015-07-09 12:03:51

#### LeetCode232——Implement Queue using Stacks

2015-07-31 16:48:03

#### LeetCode 225: Implement Stack using Queues

2015-06-13 14:58:28

#### implement a queue by using two stacks

2012-08-29 10:02:32

#### Chapter3--Stacks and Queues

2013-12-17 15:08:05

## 不良信息举报

Implement Queue using Stacks