-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution232.java
More file actions
52 lines (47 loc) · 1.3 KB
/
Copy pathsolution232.java
File metadata and controls
52 lines (47 loc) · 1.3 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
import java.util.Stack;
public class solution232 {
public Stack<Integer> stackPush;
public Stack<Integer> stackPop;
public solution232() {
stackPop = new Stack<>();
stackPush = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
stackPush.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
if(stackPush.isEmpty() && stackPop.isEmpty())
{
throw new RuntimeException("1");
}
else if (stackPop.isEmpty()){
while(!stackPush.isEmpty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
/** Get the front element. */
public int peek() {
if(stackPush.isEmpty() && stackPop.isEmpty())
{
throw new RuntimeException("2");
}
else if (stackPop.isEmpty()){
while(!stackPush.isEmpty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
if(stackPop.isEmpty()&&stackPush.isEmpty()){
return true;
}else{
return false;
}
}
}