Skip to content

Commit 72ba01e

Browse files
committed
Runtime: 1 ms (Top 70.23%) | Memory: 41.5 MB (Top 77.34%)
1 parent b24d0e4 commit 72ba01e

File tree

1 file changed

+51
-50
lines changed

1 file changed

+51
-50
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
1+
// Runtime: 1 ms (Top 70.23%) | Memory: 41.5 MB (Top 77.34%)
12
class MyQueue {
23

3-
private final Deque<Integer> stack = new ArrayDeque<>();
4-
private final Deque<Integer> temp = new ArrayDeque<>();
5-
6-
/**
7-
* Initialize your data structure here.
8-
*/
9-
public MyQueue() {}
10-
11-
/**
12-
* Pushes element x to the back of the queue.
13-
*/
14-
public void push(int x) {
15-
stack.push(x);
16-
}
17-
18-
/**
19-
* @return the element at the front of the queue and remove it.
20-
*/
21-
public int pop() {
22-
while (stack.size() > 1)
23-
temp.push(stack.pop());
24-
25-
var val = stack.pop();
26-
while (!temp.isEmpty())
27-
stack.push(temp.pop());
28-
29-
return val;
30-
}
31-
32-
/**
33-
* @return the element at the front of the queue.
34-
*/
35-
public int peek() {
36-
while (stack.size() > 1)
37-
temp.push(stack.pop());
38-
39-
var val = stack.peek();
40-
while (!temp.isEmpty())
41-
stack.push(temp.pop());
42-
43-
return val;
44-
}
45-
46-
/**
47-
* @return true if the queue is empty, false otherwise.
48-
*/
49-
public boolean empty() {
50-
return stack.isEmpty();
51-
}
52-
}
4+
private final Deque<Integer> stack = new ArrayDeque<>();
5+
private final Deque<Integer> temp = new ArrayDeque<>();
6+
7+
/**
8+
* Initialize your data structure here.
9+
*/
10+
public MyQueue() {}
11+
12+
/**
13+
* Pushes element x to the back of the queue.
14+
*/
15+
public void push(int x) {
16+
stack.push(x);
17+
}
18+
19+
/**
20+
* @return the element at the front of the queue and remove it.
21+
*/
22+
public int pop() {
23+
while (stack.size() > 1)
24+
temp.push(stack.pop());
25+
26+
var val = stack.pop();
27+
while (!temp.isEmpty())
28+
stack.push(temp.pop());
29+
30+
return val;
31+
}
32+
33+
/**
34+
* @return the element at the front of the queue.
35+
*/
36+
public int peek() {
37+
while (stack.size() > 1)
38+
temp.push(stack.pop());
39+
40+
var val = stack.peek();
41+
while (!temp.isEmpty())
42+
stack.push(temp.pop());
43+
44+
return val;
45+
}
46+
47+
/**
48+
* @return true if the queue is empty, false otherwise.
49+
*/
50+
public boolean empty() {
51+
return stack.isEmpty();
52+
}
53+
}

0 commit comments

Comments
 (0)