Skip to content

Commit 30d2a93

Browse files
committed
Time: 0 ms (100.00%) | Memory: 41.2 MB (40.09%) - LeetSync
1 parent dfe7357 commit 30d2a93

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class MyQueue {
2+
private Stack<Integer> s1;
3+
private Stack<Integer> s2;
4+
5+
public MyQueue() {
6+
s1 = new Stack<>();
7+
s2 = new Stack<>();
8+
}
9+
10+
public void push(int x) {
11+
while (!s1.isEmpty()) {
12+
s2.push(s1.pop());
13+
}
14+
s1.push(x);
15+
while (!s2.isEmpty()) {
16+
s1.push(s2.pop());
17+
}
18+
}
19+
20+
public int pop() {
21+
return s1.pop();
22+
}
23+
24+
public int peek() {
25+
return s1.peek();
26+
}
27+
28+
public boolean empty() {
29+
return s1.isEmpty();
30+
}
31+
}
32+
33+
34+
/**
35+
* Your MyQueue object will be instantiated and called as such:
36+
* MyQueue obj = new MyQueue();
37+
* obj.push(x);
38+
* int param_2 = obj.pop();
39+
* int param_3 = obj.peek();
40+
* boolean param_4 = obj.empty();
41+
*/

0 commit comments

Comments
 (0)