-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImplementQueueUsingStacks.java
More file actions
71 lines (57 loc) · 2.02 KB
/
Copy pathImplementQueueUsingStacks.java
File metadata and controls
71 lines (57 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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.
// See: https://leetcode.com/problems/implement-queue-using-stacks/
// See: https://leetcode.com/problems/implement-queue-using-stacks/discuss/583715/Java-O(1)-time-for-peek()
package leetcode.design;
import java.util.Stack;
public class ImplementQueueUsingStacks {
class MyQueue {
private Stack<Integer> s1;
private Stack<Integer> s2;
private int topElement;
/** Initialize your data structure here. */
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
if (s1.isEmpty()) topElement = x;
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
int size = s1.size();
for (int i = 0; i < size - 1; i++)
s2.push(s1.pop());
int head = s1.pop();
if (!s2.isEmpty()) topElement = s2.peek();
for (int i = 0; i < size - 1; i++)
s1.push(s2.pop());
return head;
}
/** Get the front element. */
public int peek() {
return topElement;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return s1.isEmpty();
}
}
public static void main(String[] args) {
MyQueue q = new ImplementQueueUsingStacks().new MyQueue();
q.push(2);
q.push(3);
q.push(4);
q.push(5);
System.out.println(q.pop());
System.out.println(q.pop());
System.out.println(q.pop());
System.out.println(q.peek());
System.out.println(q.peek());
}
}